百度地图SDK 踩坑之旅------百度定位OnReceiveLocation没回调,什么都没有。
来源:互联网 发布:apache nginx 比较 编辑:程序博客网 时间:2024/05/21 08:03
百度地图定位SDK在各位开发者手里捣弄的机会应该蛮多的。但是我们的百度SDK用起来并没有想像中的顺手,下面这个坑我估计很多同胞遇到过。按照BAIDU提供的Demo定位是没有问题的,但是我移植过来为什么OnReceiveLocation没有回调呢?我已经注册监听了,权限也配置了,服务也添加了,怎么没有反应呢?先别急,往下看:
- Application
@Override public void onCreate() { super.onCreate(); if(appcontext == null){ appcontext = getApplicationContext(); } //初始化定位服务 locationService = new LocationService(appcontext); SDKInitializer.initialize(getApplicationContext()); }
- JNILib
jar包
AndroidManifest.xml配置
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xxxx.xxxxx" android:versionCode="1" android:versionName="2.0.170524"> <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19"/> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > </uses-permission> <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" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" > </uses-permission> <uses-permission android:name="android.permission.READ_LOGS" > </uses-permission> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <application android:name=".application.MyApplication" android:allowBackup="true" android:icon="@drawable/app_logo" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".activity.SplashActivity" android:label="@string/app_name" android:screenOrientation="landscape" android:theme="@android:style/Theme.Light.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".activity.HomeActivity" android:theme="@android:style/Theme.Light.NoTitleBar" android:launchMode="singleInstance" /> <activity android:name=".activity.BaseActivity" android:theme="@android:style/Theme.Light.NoTitleBar"/> <activity android:name=".activity.DataTestActivity" android:theme="@android:style/Theme.Light.NoTitleBar"/> <service android:name=".services.PrintService"/> <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> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="bNVv3eM3RIVAz9DuSRbsFIlp5Wd44QzY" /> <receiver android:name=".broadcast.BootRunCast"> <intent-filter android:priority="995"> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> <receiver android:name=".broadcast.ScreenReciver"> <intent-filter android:priority="1000"> <action android:name="android.intent.action.USER_PRESENT"/> </intent-filter> </receiver> <activity android:name=".activity.UnLockInputPwd" android:launchMode="singleInstance" android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"/> </application></manifest>
- Service服务
package com.xxxx.xxxx.services;import android.content.Context;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.location.LocationClientOption.LocationMode;/** * * @author baidu * */public class LocationService { private LocationClient client = null; private LocationClientOption mOption,DIYoption; private Object objLock = new Object(); /*** * * @param locationContext */ public LocationService(Context locationContext){ synchronized (objLock) { if(client == null){ client = new LocationClient(locationContext); client.setLocOption(getDefaultLocationClientOption()); } } } /*** * * @param listener * @return */ public boolean registerListener(BDLocationListener listener){ boolean isSuccess = false; if(listener != null){ client.registerLocationListener(listener); isSuccess = true; } return isSuccess; } public void unregisterListener(BDLocationListener listener){ if(listener != null){ client.unRegisterLocationListener(listener); } } /*** * * @param option * @return isSuccessSetOption */ public boolean setLocationOption(LocationClientOption option){ boolean isSuccess = false; if(option != null){ if(client.isStarted()) client.stop(); DIYoption = option; client.setLocOption(option); } return isSuccess; } public LocationClientOption getOption(){ return DIYoption; } /*** * * @return DefaultLocationClientOption */ public LocationClientOption getDefaultLocationClientOption(){ if(mOption == null){ mOption = new LocationClientOption(); mOption.setLocationMode(LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 mOption.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系,如果配合百度地图使用,建议设置为bd09ll; mOption.setScanSpan(0);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 mOption.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 mOption.setIsNeedLocationDescribe(true);//可选,设置是否需要地址描述 mOption.setNeedDeviceDirect(false);//可选,设置是否需要设备方向结果 mOption.setLocationNotify(false);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果 mOption.setIgnoreKillProcess(true);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死 mOption.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” mOption.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 mOption.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集 mOption.setIsNeedAltitude(false);//可选,默认false,设置定位时是否需要海拔信息,默认不需要,除基础定位版本都可用 } return mOption; } public void start(){ synchronized (objLock) { if(client != null && !client.isStarted()){ client.start(); } } } public void stop(){ synchronized (objLock) { if(client != null && client.isStarted()){ client.stop(); } } } public boolean requestHotSpotState(){ return client.requestHotSpotState(); }}
- 开启定位服务在需要触发的位置调用
private void queryPostion() { //开启定位服务 // -----------location config ------------ myapplication = (MyApplication) getActivity().getApplication(); locationService = myapplication.locationService; //获取locationservice实例,建议应用中只初始化1个location实例,然后使用,可以参考其他示例的activity,都是通过此种方式获取locationservice实例的 locationService.registerListener(mListener); //注册监听 int type = getActivity().getIntent().getIntExtra("from", 0); if (type == 0) { locationService.setLocationOption(locationService.getDefaultLocationClientOption()); } else if (type == 1) { locationService.setLocationOption(locationService.getOption()); } //开始定位服务 locationService.start(); }
- 结果回调
/***** * 定位结果回调,重写onReceiveLocation方法 */ private BDLocationListener mListener = new BDLocationListener() { @Override public void onReceiveLocation(BDLocation location) { Log.d("hlhlocation", "onReceiveLocation: location"+location); if (null != location && location.getLocType() != BDLocation.TypeServerError) { if (user != null) { StringBuffer sb = new StringBuffer(256); sb.append("time : "); /** * 时间也可以使用systemClock.elapsedRealtime()方法 获取的是自从开机以来,每次回调的时间; * location.getTime() 是指服务端出本次结果的时间,如果位置不发生变化,则时间不变 */ sb.append(location.getTime()); sb.append("\nlocType : ");// 定位类型 sb.append(location.getLocType()); sb.append("\nlocType description : ");// *****对应的定位类型说明***** sb.append(location.getLocTypeDescription()); sb.append("\nlatitude : ");// 纬度 user.setLatitude(String.valueOf(location.getLatitude())); sb.append(location.getLatitude()); sb.append("\nlontitude : ");// 经度 user.setLongitude(String.valueOf(location.getLongitude())); 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 : ");// 地址信息 user.setPosition(location.getAddrStr()); sb.append(location.getAddrStr()); sb.append("\nUserIndoorState: ");// *****返回用户室内外判断结果***** sb.append(location.getUserIndoorState()); sb.append("\nDirection(not all devices have value): "); sb.append(location.getDirection());// 方向 sb.append("\nlocationdescribe: "); sb.append(location.getLocationDescribe());// 位置语义化信息 sb.append("\nPoi: ");// POI信息 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("\ngps status : "); sb.append(location.getGpsAccuracyStatus());// *****gps质量判断***** sb.append("\ndescribe : "); sb.append("gps定位成功"); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果 // 运营商信息 if (location.hasAltitude()) {// *****如果有海拔高度***** sb.append("\nheight : "); sb.append(location.getAltitude());// 单位:米 } 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("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机"); } ToastUtils.showTaost(getActivity(), sb.toString()); Log.d("hlhlocal", "onReceiveLocation: localpostion" + sb.toString()); Log.d("hlhlocal", "onReceiveLocation: localuser"+user.toString()); //存储定位信息 mainFgController.sendAsyncMessage(ConstData.MAINFG_SAVEADDR_MODEL,user); } } } public void onConnectHotSpotMessage(String s, int i) { } };
- 坑(OnReceiveLocation没有回调)去看看你的 AndroidManifest.xml配置。
<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="bNVv3eM3RIVAz9DuSRbsFIlp5Wd44QzY" />
赶紧去百度API重新申请AK咯———-
阅读全文
0 0
- 百度地图SDK 踩坑之旅------百度定位OnReceiveLocation没回调,什么都没有。
- 百度sdk定位监听 onReceiveLocation没有执行?
- Android百度地图定位API onReceiveLocation没有调用?
- 百度地图之SDK定位
- 百度地图无法定位,即无法调用onReceiveLocation。
- 关于百度地图定位 onReceiveLocation执行两次的问题
- 百度地图sdk踩坑之旅
- 百度SDK地图定位
- 定位 -- 百度地图SDK
- 集成百度地图bug,debug得到的结果是离线定位,但是什么都没有返回
- 百度地图定位SDK 之构想
- 百度地图SDK定位Location
- Android 百度地图SDK定位
- 百度地图之定位
- 百度地图之定位
- 百度地图之定位
- 百度地图之定位
- 百度地图之定位
- Java的JAR文件入门篇
- javascript-->>去重
- 打劫房屋II
- POJ 2025 Cubic Rube 笔记
- C++:民主的产物
- 百度地图SDK 踩坑之旅------百度定位OnReceiveLocation没回调,什么都没有。
- 堆——洛谷 P1334 瑞瑞的木板
- tcpdump抓包命令
- #1 介绍
- 单片机中的延时函数
- 第八章 胸有成竹
- Hibernate下单表操作-组建属性
- 正则表达式
- HDU-3350 #define is unsafe(dfs 解析字符串)