為什么室內(nèi)沒有l(wèi)ocation呢?
創(chuàng)新互聯(lián)公司主要從事網(wǎng)站設計、成都網(wǎng)站制作、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務襄汾,十載網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:028-86922220
因為我們開發(fā)的時候幾乎肯定都是在室內(nèi)的,這個時候衛(wèi)星你是搜索不到的,所以必然是定位不了的,所以系統(tǒng)如何將位置信息通知給你的程序。所以要從根本上解決這個問題,就要解決位置信息獲取問題。
那么我來告訴大家,只有NETWORK_PROVIDER這種模式才是室內(nèi)定位可靠的方式,就是當location為null的時候只要用這個,NETWORK_PROVIDER。
不過直接用大家也是用不了的,為啥呢,因為大部分廠商也不會用google的服務,這種定位方式默認是沒法用的。那怎么辦?好辦,找個替代的服務商就可以了,百度或者高德的位置信息sdk就可以解決這個問題。它的基本原理在上面已經(jīng)提到過了,就是搜集你的wifi節(jié)點信息和你的手機基站信息來定位。
本篇文章我們來用百度解決。
用百度位置定位SDK
SDK下載:http://lbsyun.baidu.com/sdk/download
SDK使用:
1. 申請百度的服務密鑰,具體操作步驟見官網(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);//設置發(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)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站標題:AndroidGPS室內(nèi)定位問題的解決方法(location為null)
網(wǎng)站網(wǎng)址:http://www.rwnh.cn/article6/gcggig.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、小程序開發(fā)、面包屑導航、網(wǎng)站改版、域名注冊、品牌網(wǎng)站設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)