百度定位sdk使用

由于在项目中用到百度定位功能,所以就去集成了定位sdk.集成百度定位sdk很简单,几乎上百度的开发者平台按照步骤就可以了。

步骤

先去百度地图开发平台注册账号,注册号后进行到android定位sdk,最新的定位功能将分为四个模块,每个模块可以单独使用。

在使用前,需先申请密钥(AK)才可使用,所以按照要求去做。创建应用,然后将包名,应用名称,开发版SHA1等信息。其中SHA1在测试时与开发与发布的暂时填写一样即可。SHA1文档中也有介绍,如果你输入命令行出现提示:不是内部命令,说明没用配置路径。输入android时输入即可,命令行界面不会显示。获取SHA1后将信息填写完整生成对应的AK.

获取发布版和开发板的SHA1:
这里写图片描述

开发板按照百度文档要求是可获得的,
说说获取发布版的SHA1其实是一个道理:

cmd进入命令行:cd.android,
这里写图片描述

可以在计算中中找到这个文件夹,看看文件夹下有什么,发现有debug.keystore这个debug签名文件,如图所示我把项目所需签名文件xxx.jks放到此文件夹下。
这里写图片描述

接着上面命令行步骤,输入keytool -list -v -keystore xxx.jks
这里写图片描述

然后输入密钥库口令,点击enter
找到相对应的别名,对应的找到SHA1
这里写图片描述

接下来就按照开发文档中介绍的去配置。
使用也是相当简单,最好在app中去初始化例如:

public class RKAPP extends Application {
    public static Context mContext;
    public static Handler mainHandler;
    public static LocationClient mLocationClient = null;
    public MyLocationListener myLocationListener;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;

        myLocationListener = new MyLocationListener(getApplicationContext());
        mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
        mLocationClient.registerLocationListener(myLocationListener);
        initLocation();
        }

 private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
        option.setCoorType("bd09ll");// 可选,默认gcj02,设置返回的定位结果坐标系
        // int span=1000;
        // option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);// 可选,设置是否需要地址信息,默认不需要
        option.setOpenGps(true);// 可选,默认false,设置是否使用gps
        option.setLocationNotify(true);// 可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);// 可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);// 可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);// 可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
        option.SetIgnoreCacheException(false);// 可选,默认false,设置是否收集CRASH信息,默认收集
        option.setEnableSimulateGps(false);// 可选,默认false,设置是否需要过滤gps仿真结果,默认需要
        mLocationClient.setLocOption(option);
    }
}
public class MyLocationListener implements BDLocationListener {


    private SharePreferencesUtil preferencesUtil;

    public MyLocationListener(Context applicationContext) {
        // TODO Auto-generated constructor stub
        preferencesUtil = new SharePreferencesUtil(applicationContext);
    }

    @Override
    public void onReceiveLocation(BDLocation location) {
        // TODO Auto-generated method stub
        if (null != location && location.getLocType() != BDLocation.TypeServerError) {
            StringBuffer sb = new StringBuffer(256);
            sb.append("time : ");
           /* *
             * 时间也可以使用systemClock.elapsedRealtime()方法 获取的是自从开机以来,每次回调的时间;
             * location.getTime() 是指服务端出本次结果的时间,如果位置不发生变化,则时间不变
             */
            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());
            sb.append("\nCountryCode : ");
            sb.append(location.getCountryCode());
            sb.append("\nCountry : ");
            sb.append(location.getCountry());
            sb.append("\ncitycode : ");
            sb.append(location.getCityCode());
            sb.append("\ncity : ");
            sb.append(location.getCity());
            sb.append("\nDistrict : ");
            sb.append(location.getDistrict());
            sb.append("\nStreet : ");
            sb.append(location.getStreet());
            sb.append("\naddr : ");
            sb.append(location.getAddrStr());
            sb.append("\nDescribe: ");
            sb.append(location.getLocationDescribe());
            sb.append("\nDirection(not all devices have value): ");
            sb.append(location.getDirection());
            sb.append("\nPoi: ");

            Map<String, String> map = new HashMap<String, String>();
            map.put("location",location.getCity()+location.getDistrict()+location.getStreet());
//            map.put("location",location.getCity()+location.getDistrict()+location.getStreet()+";"+location.getLatitude()
//                    +";"+location.getLongitude());
            map.put("Latitude",location.getLatitude()+"");
            map.put("Longitude",location.getLongitude()+"");
            map.put("code",location.getLocType()+"");//
            preferencesUtil.add(map);
            if (location.getPoiList() != null && !location.getPoiList().isEmpty()) {
                for (int i = 0; i < location.getPoiList().size(); i++) {
                    Poi poi = (Poi) location.getPoiList().get(i);
                    sb.append(poi.getName() + ";");
                }
            }
            if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());// 单位:km/h
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
                sb.append("\nheight : ");
                sb.append(location.getAltitude());// 单位:米
                sb.append("\ndescribe : ");
                sb.append("gps定位成功");
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
                // 运营商信息
                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号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
            } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
                sb.append("\ndescribe : ");
                sb.append("网络不同导致定位失败,请检查网络是否通畅");
            } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
                sb.append("\ndescribe : ");
                sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
            }
//            Log.e("tag", sb.toString());
            LogUtils.wyjlog("定位结果"+location.getLocType()+"   :"+sb.toString());
        }
    }

    @Override
    public void onConnectHotSpotMessage(String s, int i) {

    }

}

这里我使用preferencesUtil工具类集合将数据保存下来,用于需要用的时候取出来。

要注意如果你得定位只成功一次后面又不定位了多半是key的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值