百度地图中的事件

来源:互联网 发布:东野圭吾知乎 编辑:程序博客网 时间:2024/06/04 19:50

写这篇文字我想了很久(主要是怕自己能力有限误导大家)

因为本人是一个android才学习不到一个月的菜鸟  --呵呵

其实关于百度地图的介绍  在百度开发者里面都介绍的很详细了(建议大家多看看 多看几遍)

我在这里只不过是在百度地图的基础上  加上自己的一些学习感悟

百度地图中的一共有7类

1.一般监听事件MKGeneralListener 主要用在 BMapManager的初始化中

2.地图监听事件MKMapTouchListener(上一个例子有用到,这里就不讲了 这个可以实现截图(在我的工程中 使用这个我们实现了我的足迹到新浪微博的分享))

3.云检索事件(其本质是发送个百度服务器一共http命令,然后百度服务器给你返回一共JSON格式的数据  只不过百度把这个过程封装到她的云检索SDK中了 这样更加的方便开发者使用  其实我们完全可以直接动手写http和JSON的解析--其实我在自己的工程使用LBS云 确实也是自己写的 因为我要在android客服端为云上创建数据 但是百度没有相关的调用  所以没办法  只有自己动手写了  其实写完了感觉也不复杂)

4.地图点击事件(单击 双击 长按)

5.离线地图事件(这个自己还没弄过)

6.弹出窗口事件(pop事件)(这个很有用 用好了可以实现很多强大的功能 *重点)

7.搜索事件(本质和云检索一样)

对于第二个事件 分享一端自己工程中的可以分享到新浪微博的代码(这段代码大家不能直接导入工程 因为分享到新浪微博还要做很多事件(比如取得新浪的认证 封装新浪的分享函数到自己的工程) 我这个自己给大家提示思路  希望大家能够有更好的创意)

MKMapViewListener mapListener = new MKMapViewListener() {@Overridepublic void onMapMoveFinish() {}@Overridepublic void onMapAnimationFinish() {}@SuppressLint("SimpleDateFormat")@Overridepublic void onGetCurrentMap(Bitmap arg0) {progress.setVisibility(View.GONE);final Bitmap bitmap = arg0;handler.post(new Runnable() {@Overridepublic void run() {try{SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");String date = sDateFormat.format(new java.util.Date());//Environment.getExternalStorageDirectory() + "/DCIM/Camera");final String path = Environment.getExternalStorageDirectory() + "/DCIM/Camera/路线" + date + ".jpeg";// 截图文件保存路径File photodir = new File(path);FileOutputStream fout;if (photodir.exists()) {photodir.delete();}try {photodir.createNewFile();fout = new FileOutputStream(photodir);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);fout.flush();fout.close();} catch (Exception e) {Toast.makeText(MainActivity.this, e.getMessage(),Toast.LENGTH_LONG).show();}new AlertDialog.Builder(MainActivity.this).setTitle("截屏成功,分享给好友?").setPositiveButton("分享",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {// 跳转到新浪微博或其他的分享界面// 图片路径在变量path里Intent sendfilepath = new Intent(MainActivity.this,PhotoWeiboActivity.class);sendfilepath.putExtra("picPath", path);//picPath为所要分享的图片存放路径,“picPath”不用改。startActivity(sendfilepath);}}).setNegativeButton("暂不分享!", null).show();}catch(Exception e){Toast.makeText(getApplicationContext(), "截屏出错了", Toast.LENGTH_SHORT).show();}}});}@Overridepublic void onClickMapPoi(MapPoi arg0) {}};

弹出窗口事件我在分享一端代码(这段代码很有价值,希望大家认真领悟)

// 弹出泡泡图层PopupOverlay pop = new PopupOverlay(mapView, new PopupClickListener() {@Overridepublic void onClickedPopup(int arg0) {// Toast.makeText(getApplicationContext(),// "跳转到通话记录",Toast.LENGTH_SHORT).show();if (Type == 3) {// 群组跳转Intent intent = new Intent(MainActivity.this,TeamChatActivity.class);Bundle bundle = new Bundle();bundle.putString("teamId", tag);bundle.putString("teamName", tag);intent.putExtras(bundle); startActivity(intent);} else if (Type == 1) {Intent intent = new Intent(MainActivity.this,ChatActivity.class);Bundle bundle = new Bundle();bundle.putString("id", opid);bundle.putString("chat_name", name);intent.putExtras(bundle);startActivity(intent);}}});TextView popupText = null;// 泡泡viewView viewCache = null;// 处理点击事件,弹出泡泡mapView.getOverlays().remove(pop);LayoutInflater factory = LayoutInflater.from(MainActivity.this);// 获得自定义的对话框viewCache = factory.inflate(R.layout.marker_pop, null);popupText = (TextView) viewCache.findViewById(R.id.text_pop);popupText.setBackgroundResource(R.drawable.popup);if (str.length() >= 35) {str = str.substring(0, 35) + "…";}popupText.setText(str);pop.showPopup(BMapUtil.getBitmapFromView(popupText), ptstart, 8);

最后分享一段我最自豪的代码(这段代码是我想破了脑子才实现的  自豪啊)

class listoverlay extends ItemizedOverlay<OverlayItem> {// 用MapView构造ItemizedOverlaypublic listoverlay(Drawable mark, MapView mapView) {super(mark, mapView);}protected boolean onTap(int index) {// Toast.makeText(getApplicationContext(), "ontap",// Toast.LENGTH_SHORT).show();// 在此处理item点击事件if (index >= perlist.size()||kkkkk>1) {// 点击自己位置marker,不做任何处理return false;}current = index;otherinfo(perlist.get(current));return true;}public boolean onTap(GeoPoint pt, MapView mapView) {// 在此处理MapView的点击事件,当返回 true时PopupOverlay pop = new PopupOverlay(mapView, null);mapView.getOverlays().remove(pop);final ArrayList<person>plistList=new ArrayList<person>();List<String> listName = new ArrayList<String>(); // 保存在一定范围内的人名int x = pt.getLatitudeE6();int y = pt.getLongitudeE6();Point point = mapView.getProjection().toPixels(new GeoPoint(x, y), null);x = point.x;y = point.y;//int x = (int)event.getX();//int y = (int)event.getY();Projection p=mapView.getProjection();for (int i = 0; i <perlist.size(); i++) {Point sp=p.toPixels(new GeoPoint((int)(perlist.get(i).getLatitude()*1e6), (int)(perlist.get(i).getLongitude()*1e6)),null);Double dis = Math.sqrt((sp.x-x)*(sp.x-x)+(sp.y-y)*(sp.y-y));if (dis <= 25){ // 距离小于10像素//显示到listview里面plistList.add(perlist.get(i));listName.add(perlist.get(i).getNameString());}}final String[] addrs = listName.toArray(new String[0]);if(addrs.length > 1){kkkkk=2;AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("可能要找的人").setItems(addrs, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {person pon = plistList.get(which);if(pon.getId() == per.getId()){myinfo(pon);}else{otherinfo(pon);}}}).create();dialog.show();}else{kkkkk=1;}super.onTap(pt, mapView);return false;}}

呵呵  分享给大家三段经典的代码(尽管大家都不能直接导入到工程 但是希望能够借此来激发大家的无限创意)