今天自己学着集成百度地图发现百度存在如下几个不足
言归正传
学习百度地图,最好先看下它的demo,在android它总共有三个demo,其中BaiduMapSDKDemo是比较值得好好地学习的,红色箭头所示,libs中的api manifest布局文件中的一些权限可以直接放入自己的项目中,当然可以根据自己项目的需求把不需要的libs包删除(记得修改百度秘钥为自己的)下图是怎样创建百度秘钥的方法
在运行的时候记得改变秘钥的值
附下载百度sdk和demo
2016.4.22 总结今天学习的到的内容 ,重新的百度地图的集成流程有了一定的了解,认识了他的复杂性,不熟悉百度地图集成的新手往往会绕弯路,浪费大量的时间,要把百度集成到自己的项目,要一步一步来,不可能一下子就集成所有想要的功能,多研究demo和借鉴其他人更好的demo。
2016.4.23 今天的学习有些暴力,就是直接参照百度sample,把libs和一些配置文件,java文件拷贝,同时注意读懂百度的一些图 是关键,知道图代表的意思(依次点击显示的图片)。
相关代码
定位 地图
package com.pc.baidutest; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; //import android.widget.RadioGroup; import android.widget.SeekBar; import android.widget.Toast; //import android.widget.RadioGroup.OnCheckedChangeListener; import java.util.ArrayList; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.GroundOverlayOptions; import com.baidu.mapapi.map.InfoWindow; import com.baidu.mapapi.map.MapStatus; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.Marker; import com.baidu.mapapi.map.MarkerOptions; import com.baidu.mapapi.map.MyLocationConfiguration; import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.map.OverlayOptions; import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener; import com.baidu.mapapi.map.BaiduMap.OnMarkerDragListener; import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.model.LatLngBounds; /** * 此demo用来展示如何结合定位SDK实现定位,并使用MyLocationOverlay绘制定位位置 同时展示如何使用自定义图标绘制并点击时弹出泡泡 */ public class LocationDemo extends Activity { // 定位相关 LocationClient mLocClient; public MyLocationListenner myListener = new MyLocationListenner(); private LocationMode mCurrentMode; BitmapDescriptor mCurrentMarker; private static final int accuracyCircleFillColor = 0xAAFFFF88; private static final int accuracyCircleStrokeColor = 0xAA00FF00; MapView mMapView; BaiduMap mBaiduMap; // UI相关 // OnCheckedChangeListener radioButtonListener; Button requestLocButton; boolean isFirstLoc = true; // 是否首次定位 // 添加覆盖物的 点 /** * MapView 是地图主控件 */ private Marker mMarkerA; private Marker mMarkerB; private Marker mMarkerC; private Marker mMarkerD; private InfoWindow mInfoWindow; private SeekBar alphaSeekBar = null; private CheckBox animationBox = null; // 初始化全局 bitmap 信息,不用时及时 recycle BitmapDescriptor bdA = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka); BitmapDescriptor bdB = BitmapDescriptorFactory.fromResource(R.drawable.icon_markb); BitmapDescriptor bdC = BitmapDescriptorFactory.fromResource(R.drawable.icon_markc); BitmapDescriptor bdD = BitmapDescriptorFactory.fromResource(R.drawable.icon_markd); BitmapDescriptor bd = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding); // BitmapDescriptor bdGround = BitmapDescriptorFactory.fromResource(R.drawable.ground_overlay); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_location); requestLocButton = (Button) findViewById(R.id.button1); mCurrentMode = LocationMode.NORMAL; requestLocButton.setText("普通"); OnClickListener btnClickListener = new OnClickListener() { public void onClick(View v) { switch (mCurrentMode) { case NORMAL: requestLocButton.setText("跟随"); mCurrentMode = LocationMode.FOLLOWING; mBaiduMap.setMyLocationConfigeration( new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker)); break; case COMPASS: requestLocButton.setText("普通"); mCurrentMode = LocationMode.NORMAL; mBaiduMap.setMyLocationConfigeration( new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker)); break; case FOLLOWING: requestLocButton.setText("罗盘"); mCurrentMode = LocationMode.COMPASS; mBaiduMap.setMyLocationConfigeration( new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker)); break; default: break; } } }; requestLocButton.setOnClickListener(btnClickListener); // 地图初始化 mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 定位初始化 mLocClient = new LocationClient(this); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(1000); mLocClient.setLocOption(option); mLocClient.start(); // 显示覆盖物 initOverlay(); // 覆盖物的点击事件 mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() { public boolean onMarkerClick(final Marker marker) { //这里的button是图标的背景 Button button = new Button(getApplicationContext()); button.setBackgroundResource(R.drawable.popup); OnInfoWindowClickListener listener = null; //标记marker点击的效果是移动位置 if (marker == mMarkerA || marker == mMarkerD) { button.setText("更改位置"); listener = new OnInfoWindowClickListener() { public void onInfoWindowClick() { LatLng ll = marker.getPosition(); LatLng llNew = new LatLng(ll.latitude + 0.005, ll.longitude + 0.005); marker.setPosition(llNew); mBaiduMap.hideInfoWindow(); } }; //这里的button是图标的背景,但是这个背景button,会经过一个重新绘制 LatLng ll = marker.getPosition(); mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(button), ll, -47, listener); mBaiduMap.showInfoWindow(mInfoWindow); } //标记marker移动的效果是更改图标 else if (marker == mMarkerB) { button.setText("更改图标"); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { marker.setIcon(bd); mBaiduMap.hideInfoWindow(); } }); //重新绘制 LatLng ll = marker.getPosition(); mInfoWindow = new InfoWindow(button, ll, -47); mBaiduMap.showInfoWindow(mInfoWindow); } else if (marker == mMarkerC) { button.setText("删除"); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { //删除了marker不再显示 marker.remove(); mBaiduMap.hideInfoWindow(); } }); //重新绘制 LatLng ll = marker.getPosition(); mInfoWindow = new InfoWindow(button, ll, -47); mBaiduMap.showInfoWindow(mInfoWindow); } return true; } }); } private void initOverlay() { // add marker overlay LatLng llA = new LatLng(31.382395, 120.59897); LatLng llB = new LatLng(31.482395, 120.09897); LatLng llC = new LatLng(31.582395, 120.89897); LatLng llD = new LatLng(31.682395, 120.39897); MarkerOptions ooA = new MarkerOptions().position(llA).icon(bdA).zIndex(9).draggable(true); mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA)); MarkerOptions ooB = new MarkerOptions().position(llB).icon(bdB).zIndex(9); mMarkerB = (Marker) (mBaiduMap.addOverlay(ooB)); MarkerOptions ooC = new MarkerOptions().position(llC).icon(bdC).perspective(false).anchor(0.5f, 0.5f).rotate(30) .zIndex(9); mMarkerC = (Marker) (mBaiduMap.addOverlay(ooC)); ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>(); giflist.add(bdA); giflist.add(bdB); giflist.add(bdC); MarkerOptions ooD = new MarkerOptions().position(llD).icons(giflist).zIndex(9).period(10); mMarkerD = (Marker) (mBaiduMap.addOverlay(ooD)); } /** * 定位SDK监听函数 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mMapView == null) { return; } MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(100).latitude(location.getLatitude()).longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatus.Builder builder = new MapStatus.Builder(); builder.target(ll).zoom(18.0f); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); } } public void onReceivePoi(BDLocation poiLocation) { } } @Override protected void onPause() { mMapView.onPause(); super.onPause(); } @Override protected void onResume() { mMapView.onResume(); super.onResume(); } @Override protected void onDestroy() { // 退出时销毁定位 mLocClient.stop(); // 关闭定位图层 mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); mMapView = null; // 回收 bitmap 资源 bdA.recycle(); bdB.recycle(); bdC.recycle(); bdD.recycle(); bd.recycle(); // bdGround.recycle(); super.onDestroy(); } }
Application类
package com.pc.baidutest; import com.baidu.mapapi.SDKInitializer; import android.app.Application; public class PetrolStationApplication extends Application { @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); SDKInitializer.initialize(getApplicationContext()); } }
manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pc.baidutest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <!-- 百度需要的权限 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <!-- 这个权限用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 这个权限用于访问GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 用于读取手机当前的状态 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 访问网络,网络定位需要上网 --> <uses-permission android:name="android.permission.INTERNET" /> <application android:name = "com.pc.baidutest.PetrolStationApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="4E8HXxnltyT9vIKKM1YivSTdhimGW1Ce" /> <activity android:name=".LocationDemo" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
下载链接 http://pan.baidu.com/s/1dFrxxz7
参考博文 http://blog.csdn.net/crazy1235/article/details/43898451