Android GPS室內(nèi)定位問題的解決方法(location為null)
為什么室內(nèi)沒有l(wèi)ocation呢?
因為我們開發(fā)的時候幾乎肯定都是在室內(nèi)的,這個時候衛(wèi)星你是搜索不到的,所以必然是定位不了的,所以系統(tǒng)如何將位置信息通知給你的程序。所以要從根本上解決這個問題,就要解決位置信息獲取問題。
那么我來告訴大家,只有NETWORK_PROVIDER這種模式才是室內(nèi)定位可靠的方式,就是當location為null的時候只要用這個,NETWORK_PROVIDER。
不過直接用大家也是用不了的,為啥呢,因為大部分廠商也不會用google的服務(wù),這種定位方式默認是沒法用的。那怎么辦?好辦,找個替代的服務(wù)商就可以了,百度或者高德的位置信息sdk就可以解決這個問題。它的基本原理在上面已經(jīng)提到過了,就是搜集你的wifi節(jié)點信息和你的手機基站信息來定位。
本篇文章我們來用百度解決。
用百度位置定位SDK
SDK下載:http://lbsyun.baidu.com/sdk/download
SDK使用:
1. 申請百度的服務(wù)密鑰,具體操作步驟見官網(wǎng):http://api.map.baidu.com/lbsapi/cloud/geosdk.htm
2.將上面下載的sdk文件locSDK_4.1.jar拷貝到你項目的libs下
3. 修改AndroidManifest文件,在該文件里添加如下配置
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > </service> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="xxxxx " /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
上面meta-data中value的值改為你自己的密鑰
代碼里調(diào)用sdk:
public class LocationUtil {
private final static boolean DEBUG = true;
private final static String TAG = "LocationUtil";
private static LocationUtil mInstance;
private BDLocation mLocation = null;
private MLocation mBaseLocation = new MLocation();
public static LocationUtil getInstance(Context context) {
if (mInstance == null) {
mInstance = new LocationUtil(context);
}
return mInstance;
}
Context mContext;
String mProvider;
public BDLocationListener myListener = new MyLocationListener();
private LocationClient mLocationClient;
public LocationUtil(Context context) {
mLocationClient = new LocationClient(context.getApplicationContext());
initParams();
mLocationClient.registerLocationListener(myListener);
}
public void startMonitor() {
if (DEBUG) Log.d(TAG, "start monitor location");
if (!mLocationClient.isStarted()) {
mLocationClient.start();
}
if (mLocationClient != null && mLocationClient.isStarted()) {
mLocationClient.requestLocation();
} else {
Log.d("LocSDK3", "locClient is null or not started");
}
}
public void stopMonitor() {
if (DEBUG) Log.d(TAG, "stop monitor location");
if (mLocationClient != null && mLocationClient.isStarted()) {
mLocationClient.stop();
}
}
public BDLocation getLocation() {
if (DEBUG) Log.d(TAG, "get location");
return mLocation;
}
public MLocation getBaseLocation() {
if (DEBUG) Log.d(TAG, "get location");
return mBaseLocation;
}
private void initParams() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
//option.setPriority(LocationClientOption.NetWorkFirst);
option.setAddrType("all");//返回的定位結(jié)果包含地址信息
option.setCoorType("bd09ll");//返回的定位結(jié)果是百度經(jīng)緯度,默認值gcj02
option.setScanSpan(5000);//設(shè)置發(fā)起定位請求的間隔時間為5000ms
option.disableCache(true);//禁止啟用緩存定位
option.setPoiNumber(5); //最多返回POI個數(shù)
option.setPoiDistance(1000); //poi查詢距離
option.setPoiExtraInfo(true); //是否需要POI的電話和地址等詳細信息
mLocationClient.setLocOption(option);
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null) {
return ;
}
mLocation = location;
mBaseLocation.latitude = mLocation.getLatitude();
mBaseLocation.longitude = mLocation.getLongitude();
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());
sb.append("\ncity : ");
sb.append(location.getCity());
if (location.getLocType() == BDLocation.TypeGpsLocation){
sb.append("\nspeed : ");
sb.append(location.getSpeed());
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\naddr : ");
sb.append(location.getAddrStr());
}
if (DEBUG) Log.d(TAG, "" + sb);
}
public void onReceivePoi(BDLocation poiLocation) {
}
}
public class MLocation {
public double latitude;
public double longitude;
}
}
當然別忘了在setting里將gps定位打開。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio實現(xiàn)第三方QQ登錄操作代碼
這篇文章主要介紹了Android Studio實現(xiàn)第三方QQ登錄的操作方法,本文圖文并茂給大家介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下2017-12-12
解決Android橫豎屏切換數(shù)據(jù)丟失問題的方法
這篇文章主要為大家詳細介紹了Android橫豎屏切換數(shù)據(jù)丟失問題的解決方法,感興趣的小伙伴們可以參考一下2016-05-05
打飛機游戲終極BOSS Android實戰(zhàn)打飛機游戲完結(jié)篇
打飛機游戲終極BOSS,Android實戰(zhàn)打飛機游戲完結(jié)篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
android BitmapFactory.Options使用方法詳解
這篇文章主要為大家詳細介紹了android BitmapFactory.Options使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
Android BottomSheet效果的兩種實現(xiàn)方式
這篇文章主要介紹了Android BottomSheet效果的兩種實現(xiàn)方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
flutter 輪播圖動態(tài)加載網(wǎng)絡(luò)圖片的方法
Flutter是谷歌的移動UI框架,可以快速在iOS和Android上構(gòu)建高質(zhì)量的原生用戶界面。這篇文章主要介紹了flutter 輪播圖動態(tài)加載網(wǎng)絡(luò)圖片的方法 ,需要的朋友可以參考下2019-07-07

