百度基础定位SDK集成步骤

百度定位官方网站:http://lbsyun.baidu.com/index.php?title=android-locsdk

1.导包

将百度定位demo中libs目录下的so库文件夹和相关jar包复制到我们的项目中,我做的时候5个so库文件夹和3个jar包;

2.配置 gradle

如果使用AndroidStudio开发,只是将so库文件放到libs目录下是不够的,还需要在APP的build.gradle文件中配置相关使用。在android{}中添加如下代码:

sourceSets {
            main {
                jniLibs.srcDirs = ['libs']
            }
}

3.添加 权限
注意:定位SDKv3.1版本之后,以下权限已不需要,请取消声明,否则将由于Android 5.0多帐户系统加强权限管理而导致应用安装失败。

<!-- 这个权限用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<!-- 用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!-- 访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SD卡读取权限,用户写入离线定位数据-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

4.声明服务
在清单文件中声明百度提供的定位服务。

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

5.配置百度key
SDK4.2及之后版本需要在Mainfest.xml设置Accesskey,设置有误会引起定位和地理围栏服务不能正常使用,必须进行Accesskey的正确设置。
这里需要的Accesske我们可以在百度定位官网上创建应用后拿到。配置在清单文件中:

<meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="key" />       //key:开发者申请的key

6.百度定位SDK Demo中将初始化LocationClient和配置定位SDK参数、开启停止服务等的任务放到了单独的LocateService类中。如下:

    public class LocateService {
    private Object object = new Object();
    private LocationClient client = null;
    private LocationClientOption option, userDefinedOption;
    public LocateService(Context context) {
        synchronized (object) {
            if (client == null) {
                client = new LocationClient(context);
                option = new LocationClientOption(getLocationOption());
            }
        }
    }

    public LocationClientOption getLocationOption() {
        if (option == null) {
            option = new LocationClientOption();
    //          设置定位模式为高精度定位
            option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
    //          设置定位返回坐标系,默认gcj02,如果配合百度地图使用,请设置为bd0911
            option.setCoorType("gcj02");
    //          设置发送定位请求的时间间隔,需要大于1000才能有效,默认为0,即只请求一次
            option.setScanSpan(0);
    //          定位sdk内部是一个service,并在单独进程中开启,这里设置是否在调用stop方法时是否杀死这个进程,默认为true
            option.setIgnoreKillProcess(true);
    //          设置是否需要地址信息,默认不需要
            option.setIsNeedAddress(true);
    //          设置是否需要地址描述,可在DBLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
            option.setIsNeedLocationDescribe(true);
    //          当GPS开启时,是否需要按照1次/秒的频率输出GPS结果
            option.setLocationNotify(false);
    //            是否手机crash信息,默认收集
            option.SetIgnoreCacheException(false);
    //            设置是否需要poi结果,可以在BDLocation.getPoilist里得到
            option.setIsNeedLocationPoiList(true);
    //            设置是否需要海拔信息,除基础定位外都可用,默认不需要
            option.setIsNeedAltitude(true);

        }
        return option;
    }

    /**
     * 自定义option
     *
     * @param option 用户自定义的option
     * @return 设置成功,返回true,否则返回false
     */
    public boolean setLocationOption(LocationClientOption option) {
        boolean isOk = false;
        if (option != null) {
            if (client.isStarted()) {
                client.stop();
            }
            userDefinedOption = option;
            client.setLocOption(userDefinedOption);
            isOk = true;
        }
        return isOk;
    }

    /**
     * 获取当前的定位属性
     *
     * @return 当前使用的定位属性
     */
    public LocationClientOption getOption() {
        return userDefinedOption;
    }

    /**
     * 开启定位
     */
    public void start() {
        synchronized (object) {
            if (client != null && !client.isStarted()) {
                client.start();
                LogUtils.e("service is start");
            }
        }
    }

    /**
     * 关闭定位
     */
    public void stop() {
        synchronized (object) {
            if (client != null && client.isStarted()) {
                client.stop();
                LogUtils.e("service is stop");
            }
        }
    }

    /**
     * 注册定位监听
     *
     * @param listener 等待被注册的定位监听
     * @return 注册成功返回true,否则返回false
     */
    public boolean registListener(BDLocationListener listener) {
        boolean isOk = false;
        if (listener != null) {
            client.registerLocationListener(listener);
            isOk = true;
        } else {

        }
        return isOk;
    }

    /**
     * 注销定位监听
     *
     * @param listener 当前的定位监听
     */
    public void unRegistListener(BDLocationListener listener) {
        if (listener != null) {
            client.unRegisterLocationListener(listener);
        }
    }}

 7.在application中初始化定位SDK

    //注意:这里的context需要全进程有效,所以推荐使用getApplicationContext。
      service = new LocateService(getApplicationContext());
      SDKInitializer.initialize(getApplicationContext());

8.设置监听逻辑

    public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //Receive Location
            StringBuffer sb = new StringBuffer(256);
            sb.append("time : ");
            sb.append(location.getTime());
            sb.append("\nerror code : ");
            sb.append(location.getLocType());
            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());
            sb.append("\nradius : ");
            sb.append(location.getRadius());
            if (location.getLocType() == BDLocation.TypeGpsLocation){// GPS定位结果
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());// 单位:公里每小时
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
                sb.append("\nheight : ");
                sb.append(location.getAltitude());// 单位:米
                sb.append("\ndirection : ");
                sb.append(location.getDirection());// 单位度
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
                sb.append("\ndescribe : ");
                sb.append("gps定位成功");

            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){// 网络定位结果
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
                //运营商信息
                sb.append("\noperationers : ");
                sb.append(location.getOperators());
                sb.append("\ndescribe : ");
                sb.append("网络定位成功");
            } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
                sb.append("\ndescribe : ");
                sb.append("离线定位成功,离线定位结果也是有效的");
            } else if (location.getLocType() == BDLocation.TypeServerError) {
                sb.append("\ndescribe : ");
                sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到[email protected],会有人追查原因");
            } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
                sb.append("\ndescribe : ");
                sb.append("网络不同导致定位失败,请检查网络是否通畅");
            } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
                sb.append("\ndescribe : ");
                sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
            }
            sb.append("\nlocationdescribe : ");
                sb.append(location.getLocationDescribe());// 位置语义化信息
                List<Poi> list = location.getPoiList();// POI数据
                if (list != null) {
                    sb.append("\npoilist size = : ");
                    sb.append(list.size());
                    for (Poi p : list) {
                        sb.append("\npoi= : ");
                        sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
                    }
                }
            Log.i("BaiduLocationApiDem", sb.toString());
        }

9.开启定位并注册监听

service = ((LhshAppLication) getApplication()).service;
service.registListener(listener);
service.setLocationOption(service.getLocationOption());
service.start();

猜你喜欢

转载自blog.csdn.net/baisemaque/article/details/52460648
今日推荐