国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-12-12 03:40:53
字體:
來源:轉載
供稿:網友

為什么室內沒有location呢?

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

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

用百度位置定位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);//設置發起定位請求的間隔時間為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定位打開。

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 驻马店市| 禹城市| 庆阳市| 定结县| 陵川县| 英吉沙县| 沈阳市| 郎溪县| 海伦市| 舞钢市| 萍乡市| 湘潭市| 富裕县| 阳高县| 贞丰县| 双鸭山市| 大同县| 习水县| 蒙山县| 元江| 曲水县| 福州市| 鹰潭市| 大渡口区| 赣榆县| 黄浦区| 东丰县| 宁都县| 桓台县| 勐海县| 六盘水市| 庆城县| 陆川县| 安溪县| 固镇县| 石台县| 驻马店市| 哈密市| 泰顺县| 卢氏县| 镇康县|