android百度地图开发总结

这周因为公司要求,需要开发百度地图模块。然后我通过一周的研究,终于把项目完成了。其实百度地图很简单,只要按照官方API一步一步操作就好了。因为我们公司需要实现其他的一些功能,所以拖了一周才完成。

废话不多说,直接说我遇到的百度地图常见的坑。

首先我说一下,在网上只要搜百度地图,就有很多百度地图的小demo,但是说实话有很多都是现在运行会报错的。主要原因还是在于百度地图更新就不兼容原来的版本了,所以很多几年前网上的小demo,现在即使是复制到自己的项目中,也是不正确的。

好了,我们直接说项目。首先是注册百度地图开发者账号,然后申请key,这点大家应该都知道的,也没有什么技术含量。我要说的在Manifest文件中配置百度地图的key。

<meta-data
   android:name="com.baidu.lbsapi.API_KEY"
   android:value="VgLKadEMgBQX0VHotcWFKtWADaUeH2Pi" />

我在网上看到一些demo中,说name中的Key的名字可以随便写,但是我遇到的确实name必须是com.baidu.lbsapi.API_KEY,否则他的Key值就无法通过验证。

配置完地图,我们就直接按照API一步一步的照做就好了,将sdk下载下来,移到自己的项目中,配置一下.so库,然后直接用就可以了。

这样地图就显示出来了,然后我们要做的是获取自己当前的位置,这个也没有什么难度,API说的很详细。当显示到自己位置上的时候,我们发现没有自己的标注显示在地图上。这时候我们可以自定义标注显示在上面。但是自定义标注也有不足,那就是我们在移动的时候,需要在下个位置显示的时候,就必须手动把上一个自己的位置remove掉,很麻烦。所以我们可以用百度地图定义好的位置图标。

// 显示个人位置图标
 MyLocationData.Builder builder = new MyLocationData.Builder();
 builder.latitude(location.getLatitude());
 builder.longitude(location.getLongitude());
 MyLocationData data = builder.build();
 baiduMap.setMyLocationData(data);

这样就直接显示百度地图自己的那个图标了,也不用自己在remove掉上一个位置上的了。

但是要注意,想要实现定位,还需要注册一个服务。

<service
   android:name="com.baidu.location.f"
   android:enabled="true"
   android:process=":remote" >

位置信息很好获取,然后就是方向信息了。这里我们就需要通过手机的方向感应器来获取具体的方位了。自定义类实现SensorEventListener 接口,通过这个接口我们就可以获取这些信息,当然,这个需要权限的。

public class MyOrientationListener implements SensorEventListener {

    private Context context;
    private SensorManager sensorManager;
    private Sensor sensor;

    private float lastX ; 

    private OnOrientationListener onOrientationListener ; 

    public MyOrientationListener(Context context)
    {
        this.context = context;
    }

    // 开始
    public void start() {
        // 获得传感器管理器
        sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        if (sensorManager != null) {
            // 获得方向传感器
            sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        }
        // 注册
        if (sensor != null) {//SensorManager.SENSOR_DELAY_UI
            sensorManager.registerListener(this, sensor,
                    SensorManager.SENSOR_DELAY_UI);
        }

    }

    // 停止检测
    public void stop()
    {
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {

    }

    @Override
    public void onSensorChanged(SensorEvent event)
    {
        // 接受方向感应器的类型  
        if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)  
        {  
            // 这里我们可以得到数据,然后根据需要来处理  
            float x = event.values[SensorManager.DATA_X];  
            if( Math.abs(x- lastX) > 1.0 ) {
                onOrientationListener.onOrientationChanged(x);
            }
            lastX = x ;

        }  
    }

    public void setOnOrientationListener(OnOrientationListener onOrientationListener)
    {
        this.onOrientationListener = onOrientationListener ;
    }


    public interface OnOrientationListener {
        void onOrientationChanged(float x);
    }

}
 private void initOritationListener() {
myOrientationListener = new MyOrientationListener(getApplicationContext());
myOrientationListener.setOnOrientationListener(new MyOrientationListener.OnOrientationListener() {
     @Override
     public void onOrientationChanged(float x){
            mXDirection = (int) x;//设置方向
            // 构造定位数据
             locData = new MyLocationData.Builder()
             .accuracy(mCurrentAccracy)
             // 此处设置开发者获取到的方向信息,顺时针0-360
             .direction(mXDirection)
             .latitude(mCurrentLantitude)
             .longitude(mCurrentLongitude).build();
             // 设置定位数据
             baiduMap.setMyLocationData(locData);
             //设置自己的位置和方向的显示
             MyLocationConfiguration config = new    MyLocationConfiguration(mCurrentMode, true, null);
           baiduMap.setMyLocationConfigeration(config);
             }
        });
   }

这时我们已经获取到了定位信息了,但是我们还是无法将方向的信息显示到界面上。为什么呢?因为我们自己的位置上面显示的只是自己的一个位置点。无法显示方向的变化。这时候怎么办呢?我们只需要在myOrientationListener.setOnOrientationListener里面加两行代码就OK了。

//设置自己的位置和方向的显示
   MyLocationConfiguration config = new MyLocationConfiguration(
      mCurrentMode, true, null);
   baiduMap.setMyLocationConfigeration(config);

我们在MyLocationConfiguration的第三个参数添加的是null,意思是使用默认的显示图标,我们也可以自己定义一个标志显示。只需要改变第三个参数,并且把显示个人位置的图标隐藏就好。这样就完成了,一个基本地图的显示了。
Sunanang

猜你喜欢

转载自blog.csdn.net/sun_lianqiang/article/details/56915645