官方文档其实已经相当详细了,最好结合官方demo一步一步来实现。
首先获取百度地图移动版的开发密钥,具体流程
用mac的朋友打开终端输入
keytool -list -v -keystore ~/.android/debug.keystore -aliasandroiddebugkey -storepass android -keypass android
可以拿到SHA1
提交之后就在控制台看到生成的(AK)
单独的实现地图功能只需要结合文档就行,这里说一下定位,需要定位的朋友首先需要在
AndroidManifest.xml添加
需要在build.gradle中配置SO文件的使用,如下所示
然后添加相应的权限别忘记。
然后代码,其实就是demo里面的
public class MainActivity 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; // 是否首次定位 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); //普通地图 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); 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); RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup); radioButtonListener = new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.defaulticon) { // 传入null则,恢复默认图标 mCurrentMarker = null; mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, null)); } if (checkedId == R.id.customicon) { // 修改为自定义marker mCurrentMarker = BitmapDescriptorFactory .fromResource(R.drawable.icon_geo); mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker, accuracyCircleFillColor, accuracyCircleStrokeColor)); } } }; group.setOnCheckedChangeListener(radioButtonListener); // 地图初始化 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(); } /** * 定位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; super.onDestroy(); } }大概完成后,运行会发现定位到了几内亚湾,其实就是你libs这些文件里面都缺少了一个so文件,liblocSDK6a.so,把这个文件从官方demo拿过来就行了