Android地图开发-BingMap基础教程

原文请参考:http://blog.****.net/mad1989/article/details/9733133

开端

本文皆在指引大家在自己的项目里嵌入BingMap,并可以在地图上添加覆盖物、折线等常用地图功能。

1.1 SDK 和API

http://bingmapsandroidsdk.codeplex.com/
BingMap的android版 SDK并没有集成在微软的网站里,而是放在了其开源网站:CodePlex。
该网页并没有相关的API文档以及example。


1.2 BingMapsAndroidSDK(bing.jar)

1.2.1 下载的SDK是一个android Library,该SDK并未整合成一个jar包(像百度高德地图那样),所以在project.properties添加:android.library=true;然后在自己项目properties添加library引用。

Android地图开发-BingMap基础教程

或是从网上下载一个bing.jar包,这个包是从项目中抽离出来的API,我打成了jar包,可以放在项目libs里,跟上边的方法一样的效果,那一个都可以。

Android地图开发-BingMap基础教程


1.2.2  BingMapsAndroidSDK下assets里的资源和js文件全部拷贝到自己项目下:

Android地图开发-BingMap基础教程

1.3 地图实例化

activity_main.xml

[java] view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <org.bingmaps.sdk.BingMapsView  
  7.         android:id="@+id/myBingMapview"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent" >  
  10.     </org.bingmaps.sdk.BingMapsView>  
  11.   
  12.     <LinearLayout  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentBottom="true"  
  16.         android:layout_alignParentRight="true"  
  17.         android:layout_gravity="center|bottom"  
  18.         android:background="@android:drawable/zoom_plate"  
  19.         android:paddingLeft="15dip"  
  20.         android:paddingRight="15dip" >  
  21.   
  22.         <LinearLayout  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:paddingTop="5dip" >  
  26.   
  27.             <ZoomButton  
  28.                 android:id="@+id/zoomInBtn"  
  29.                 android:layout_width="wrap_content"  
  30.                 android:layout_height="wrap_content"  
  31.                 android:background="@android:drawable/btn_plus" />  
  32.   
  33.             <ZoomButton  
  34.                 android:id="@+id/zoomOutBtn"  
  35.                 android:layout_width="wrap_content"  
  36.                 android:layout_height="wrap_content"  
  37.                 android:background="@android:drawable/btn_minus" />  
  38.         </LinearLayout>  
  39.     </LinearLayout>  
  40.   
  41. </RelativeLayout>  

MainActivity.java

[java] view plain copy
  1. mBingMapView = (BingMapsView) findViewById(R.id.myBingMapview);  
  2. inButton = (ZoomButton) findViewById(R.id.zoomInBtn);  
  3. outButton = (ZoomButton) findViewById(R.id.zoomOutBtn);  
  4. inButton.setOnClickListener(this);  
  5. outButton.setOnClickListener(this);  
  6. Coordinate coordinate = new Coordinate(39.901873116.326655);  
  7. mBingMapView.loadMap(BINGMAPVIEW_API_KEY, coordinate,Constants.DefaultGPSZoomLevel);  
  8. mBingMapView.setMapStyle(MapStyles.Auto);  
  9. mBingMapView.setCenterAndZoom(coordinate, Constants.DefaultGPSZoomLevel);  

真机效果:

Android地图开发-BingMap基础教程


使用感受:

Map加载不是特别快,手指触摸缩放地图不流畅,使用地图需要申请Key。地图体验效果明显没有IOS版好。在平板或手机上,有时加载不出地图会有如下的显示

Android地图开发-BingMap基础教程


其实这个SDK是把js代码封装成了常用的java类,java类通过调用js代码,获取mapview相关信息,js代码再把返回的内容传递给java类,看一下项目assets里的js代码和html就明白了。

Android地图开发-BingMap基础教程

1.4 EntityLayer

[java] view plain copy
  1. 示例代码:  
  2.         EntityLayer entityLayer = (EntityLayer) mBingMapView.getLayerManager()  
  3.                 .getLayerByName(Constants.DataLayers.Search);  
  4.         if (entityLayer == null) {  
  5.             entityLayer = new EntityLayer(Constants.DataLayers.Search);  
  6.         }  
  7.         entityLayer.clear();  
  8.         double longitude = Double.parseDouble("116.36212");  
  9.         double latitude = Double.parseDouble("39.946057");  
  10.         Coordinate coord = new Coordinate(latitude, longitude);  
  11.   
  12.         // 实现标记必须用到 Pushpin 来做标记。  
  13.         // PushpinOptions可以对 Pushpin所要标记的设置属性  
  14.         // opt.Icon图标 opt.Anchor点的位置  
  15.         PushpinOptions opt = new PushpinOptions();  
  16.         opt.Icon = "file:///android_asset/start.png";  
  17.         opt.Width = 50;  
  18.         opt.Height = 50;  
  19.         opt.Anchor = new Point(1110);  
  20.         Pushpin p = new Pushpin(coord, opt);  
  21.                 p.Title = "I`m a title ";//不设置title属性,不会显示infobox(吹出框)  
  22.         if (p.Location != null) {  
  23.             listCoord.add(coord);  
  24.             entityLayer.add(p);  
  25.         }  
  26.         mBingMapView.getLayerManager().addLayer(entityLayer);  
  27.         entityLayer.updateLayer();  

真机效果:

Android地图开发-BingMap基础教程

EntityLayer相当于常用的Overlay,只是添加Overlay方式跟web版类似,官网也没有相关的API说明和示例代码,添加起来比较麻烦,设置各个属性参数不是很灵活,得花费一段时间看源码。

Mapview设置EntityclickListener监听,Pushpin设置titile属性,点击Marker中的title,才能触发EntityClickListener,这个监听相当于吹出框title点击事件,如果想添加更多的监听事件,请查阅SDK 源码,如果没有想要的监听,则需要自己扩充实现,这个SDK已经好长时间没有人来维护了。

1.5 Polyline

[java] view plain copy
  1. 示例代码:  
  2.         Coordinate cd1 = new Coordinate(39.946251116.362228);  
  3.         Coordinate cd2 = new Coordinate(39.954548116.379188);  
  4.         Coordinate cd3 = new Coordinate(39.955543116.414402);  
  5.         Coordinate cd4 = new Coordinate(39.939391116.424175);  
  6.         Coordinate cd5 = new Coordinate(39.929765116.42475);        
  7.   
  8.         List<Coordinate> lists = new ArrayList<Coordinate>();  
  9.         lists.add(cd1);  
  10.         lists.add(cd2);  
  11.         lists.add(cd3);  
  12.         lists.add(cd4);  
  13.         lists.add(cd5);  
  14.           
  15.         // 在bing地图中画线所要用到 Polyline  
  16. //       PolylineOptions 是对线的属性设置  
  17. //       polylineOptions.StrokeThickness 为线的大小  
  18. //       polylineOptions.StrokeColor 线的颜色值  
  19.          Polyline routeLine = new Polyline(lists);  
  20.          PolylineOptions polylineOptions = new PolylineOptions();  
  21.          polylineOptions.StrokeThickness = 3;  
  22.          routeLine.Options = polylineOptions;  
  23.          entityLayer.add(routeLine);  
  24.          entityLayer.updateLayer();   
效果图:

Android地图开发-BingMap基础教程

1.6路径规划、逆地址解析

自带的SDK有相关的类,实现需要看代码研究,时间有限,本文就不再一一列举。
Android地图开发-BingMap基础教程


结尾
微软BingMap 对android支持不是太好,唯一指定SDK最后一次更新是2011.08,并且目前的版本,问题很多,也可以自己写js代码,转换java的代码,这样会耗费很大的精力。当前的SDK也可以实现添加marker画线,由于点击事件无法触发,吹出框自定义是个问题,当前的版本地图监听事件太少,移植过程需要不断扩展SDK。


BingMapSDK 地址:
Bing.jar地址:
(bing.jar只是我把SDK的东西打包成了一个jar包,BingMapSDK是官方的SDK,任选一个就可以了)

本文Demo地址: