欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android GPS室內定位問題的解決方法(location為null)

 更新時間:2017年02月20日 15:35:33   作者:柳湘翎Macsags  
這篇文章主要為大家詳細介紹了Android GPS室內定位問題的解決方法,location為null,具有一定的參考價值,感興趣的小伙伴們可以參考一下

為什么室內沒有l(wèi)ocation呢?

       因為我們開發(fā)的時候幾乎肯定都是在室內的,這個時候衛(wèi)星你是搜索不到的,所以必然是定位不了的,所以系統(tǒng)如何將位置信息通知給你的程序。所以要從根本上解決這個問題,就要解決位置信息獲取問題。
        那么我來告訴大家,只有NETWORK_PROVIDER這種模式才是室內定位可靠的方式,就是當location為null的時候只要用這個,NETWORK_PROVIDER。
        不過直接用大家也是用不了的,為啥呢,因為大部分廠商也不會用google的服務,這種定位方式默認是沒法用的。那怎么辦?好辦,找個替代的服務商就可以了,百度或者高德的位置信息sdk就可以解決這個問題。它的基本原理在上面已經提到過了,就是搜集你的wifi節(jié)點信息和你的手機基站信息來定位。 

        本篇文章我們來用百度解決。

用百度位置定位SDK

SDK下載:http://lbsyun.baidu.com/sdk/download
SDK使用:

1.  申請百度的服務密鑰,具體操作步驟見官網: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的值改為你自己的密鑰

  代碼里調用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");//返回的定位結果包含地址信息
 option.setCoorType("bd09ll");//返回的定位結果是百度經緯度,默認值gcj02
 option.setScanSpan(5000);//設置發(fā)起定位請求的間隔時間為5000ms
 option.disableCache(true);//禁止啟用緩存定位
 option.setPoiNumber(5); //最多返回POI個數 
 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定位打開。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論