百度地图开发中的一些问题

来源:互联网 发布:拼图工厂软件下载 编辑:程序博客网 时间:2024/06/06 08:47

一、定位功能的实现:

  1. 创建LocationClient
  2. implements BDLocationListener接口,重写onReceive方法
  3. 创建并注册listener
  4. 创建并设置LocationClientOption

LocationClientOption sample:

option.setCoorType("bd09ll");option.setIsNeedAddress(true);option.setOpenGps(true);option.setScanSpan(1000);

onReceive sample:

Log.e("mybaidu","latitude:" + location.getLatitude() + "; longtitude:" + location.getLongitude());MyLocationData data = new MyLocationData.Builder()//    .accuracy(location.getRadius())//    .latitude(location.getLatitude())//    .longitude(location.getLongitude())//     .build();mBaiduMap.setMyLocationData(data);if(isFirstIn){    LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());    MapStatusUpdate msu = MapStatusUpdateFactory.newLatLngZoom(latLng,18);    mBaiduMap.animateMapStatus(msu);    isFirstIn = false;    Toast.makeText(getContext(), location.getAddrStr(),Toast.LENGTH_SHORT).show();            }

二、NOTE:

  1. AndroidManifest.xml中添加服务(百度API文档中未提)
    <service
    android:name="com.baidu.location.f"
    android:enabled="true"
    android:process=":remote">
    <intent-filter>
    <action android:name="com.baidu.location.service_v2.2">
    </action>
    </intent-filter>
    </service>
  2. 在onResume()中进行开启定位
    mBaiduMap.setMyLocationEnabled(true);
    mLocationClient.start();
  3. 在onStop()中关闭定位
    mBaiduMap.setMyLocationEnabled(false);
    mLocationClient.stop();
  4. 在onPause()/onDestroy()中分别进行mapView的onPause()/onDestroy()

三、遇到的问题

1.打开地图显示一片白/蓝,不显示地图,原因是申请百度地图AK时,数字签名(SHA1)不正确

  • 解决方案:
    1. 使用命令行进入.android路径(c:\users\用户名.android)
    2. 开发模式下输入命令:keytool -list -v -keystore debugstore 回车,输入密码,默认是android(输入时屏幕不显示,直接打完按回车,当时被这个坑了好一会),即可获得SHA1。

2.百度定位获取经纬度正确,但是在地图上显示不正确,有一两公里的偏差,原因是忘了把option设置给mLocationClient,这个是粗心导致的,也浪费了自己好多时间。

  • 解决方案:
    • 重复多次定位,记得一定要设置option
0 0
原创粉丝点击