亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

       因?yàn)槲覀冮_(kāi)發(fā)的時(shí)候幾乎肯定都是在室內(nèi)的,這個(gè)時(shí)候衛(wèi)星你是搜索不到的,所以必然是定位不了的,所以系統(tǒng)如何將位置信息通知給你的程序。所以要從根本上解決這個(gè)問(wèn)題,就要解決位置信息獲取問(wèn)題。
        那么我來(lái)告訴大家,只有NETWORK_PROVIDER這種模式才是室內(nèi)定位可靠的方式,就是當(dāng)location為null的時(shí)候只要用這個(gè),NETWORK_PROVIDER。
        不過(guò)直接用大家也是用不了的,為啥呢,因?yàn)榇蟛糠謴S商也不會(huì)用google的服務(wù),這種定位方式默認(rèn)是沒(méi)法用的。那怎么辦?好辦,找個(gè)替代的服務(wù)商就可以了,百度或者高德的位置信息sdk就可以解決這個(gè)問(wèn)題。它的基本原理在上面已經(jīng)提到過(guò)了,就是搜集你的wifi節(jié)點(diǎn)信息和你的手機(jī)基站信息來(lái)定位。 

        本篇文章我們來(lái)用百度解決。

用百度位置定位SDK

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

1.  申請(qǐng)百度的服務(wù)密鑰,具體操作步驟見(jiàn)官網(wǎng):http://api.map.baidu.com/lbsapi/cloud/geosdk.htm
2.將上面下載的sdk文件locSDK_4.1.jar拷貝到你項(xiàng)目的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)緯度,默認(rèn)值gcj02
 option.setScanSpan(5000);//設(shè)置發(fā)起定位請(qǐng)求的間隔時(shí)間為5000ms
 option.disableCache(true);//禁止啟用緩存定位
 option.setPoiNumber(5); //最多返回POI個(gè)數(shù) 
 option.setPoiDistance(1000); //poi查詢(xún)距離 
 option.setPoiExtraInfo(true); //是否需要POI的電話和地址等詳細(xì)信息 
 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;
 }
}

當(dāng)然別忘了在setting里將gps定位打開(kāi)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論