Android地图开发之BingMap基础教程

【注意】由于BingMap此类教程在国内不太多,本文是本人花费多日通过一些国外网站论坛总结所得,如要转载,请保留文章出处,尊重一下作者,谢谢。

开端

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

1.1 SDK 和API

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

1.2BingMapsAndroidSDK(bing.jar)

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

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

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

1.3 地图实例化

activity_main.xml

<RelativeLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent" ><org.bingmaps.sdk.BingMapsViewandroid:id="@+id/myBingMapview"android:layout_width="fill_parent"android:layout_height="fill_parent" ></org.bingmaps.sdk.BingMapsView><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_gravity="center|bottom"android:background="@android:drawable/zoom_plate"android:paddingLeft="15dip"android:paddingRight="15dip" ><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingTop="5dip" ><ZoomButtonandroid:id="@+id/zoomInBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:drawable/btn_plus" /><ZoomButtonandroid:id="@+id/zoomOutBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@android:drawable/btn_minus" /></LinearLayout></LinearLayout></RelativeLayout>MainActivity.javamBingMapView = (BingMapsView) findViewById(R.id.myBingMapview);inButton = (ZoomButton) findViewById(R.id.zoomInBtn);outButton = (ZoomButton) findViewById(R.id.zoomOutBtn);inButton.setOnClickListener(this);outButton.setOnClickListener(this);Coordinate coordinate = new Coordinate(39.901873, 116.326655);mBingMapView.loadMap(BINGMAPVIEW_API_KEY, coordinate,Constants.DefaultGPSZoomLevel);mBingMapView.setMapStyle(MapStyles.Auto);mBingMapView.setCenterAndZoom(coordinate, Constants.DefaultGPSZoomLevel);真机效果:

使用感受:

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

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

1.4 EntityLayer示例代码:EntityLayer entityLayer = (EntityLayer) mBingMapView.getLayerManager().getLayerByName(Constants.DataLayers.Search);if (entityLayer == null) {entityLayer = new EntityLayer(Constants.DataLayers.Search);}entityLayer.clear();double longitude = Double.parseDouble("116.36212");double latitude = Double.parseDouble("39.946057");Coordinate coord = new Coordinate(latitude, longitude);// 实现标记必须用到 Pushpin 来做标记。// PushpinOptions可以对 Pushpin所要标记的设置属性// opt.Icon图标 opt.Anchor点的位置PushpinOptions opt = new PushpinOptions();opt.Icon = "file:///android_asset/start.png";opt.Width = 50;opt.Height = 50;opt.Anchor = new Point(11, 10);Pushpin p = new Pushpin(coord, opt);p.Title = "I`m a title ";//不设置title属性,不会显示infobox(吹出框)if (p.Location != null) {listCoord.add(coord);entityLayer.add(p);}mBingMapView.getLayerManager().addLayer(entityLayer);entityLayer.updateLayer();真机效果:辽远或偏僻的地方,而会常常想起这一次的旅行,

Android地图开发之BingMap基础教程

相关文章:

你感兴趣的文章:

标签云: