安卓定位問題

2021-07-05 18:10:23 字數 3392 閱讀 3249

大家去網上搜尋android定位location為null沒法定位問題,估計有一大堆文章介紹如何來解決,但是最後大家發現基本沒用。本文將從android定位實現原理來深入分析沒法定位原因並提出真正的解決方案。在分析之前,我們肯定得先看看android官方提供的定位sdk。

mlocationmanager = (locationmanager) getsystemservice(context.location_service);

android系統存在多種provider,分別是

gps_provider:

這個就是手機裡有gps晶元,然後利用該晶元就能利用衛星獲得自己的位置資訊。但是在室內,gps定位基本沒用,很難定位的到。

network_provider:

這個就是利用網路定位,通常是利用手機基站和wifi節點的位址來大致定位位置,

這種定位方式取決於伺服器,即取決於將基站或wif節點資訊翻譯成位置資訊的伺服器的能力。由於目前大部分android手機沒有安裝google官方的location manager庫,大陸網路也不允許,即沒有伺服器來做這個事情,自然該方法基本上沒法實現定位。

passive_provider:

使用者可以直接指定某乙個provider

string provider = mlocationmanager.getprovider(locationmanager.gps_provider);
也可以提供配置,由系統根據使用者的配置為使用者選擇乙個最接近使用者需求的provider

criteria crite = new criteria();  

crite.setaccuracy(crite.accuracy_fine); //精度

crite.setpowerrequirement(crite.power_low); //功耗型別選擇

string provider = mlocationmanager.getbestprovider(crite, true);

location location = mlocationmanager.getlocation(provider);

然後你會發現,這個返回的location永遠為null,你自然沒法定位。然後網上到處是諮詢為啥獲得的location為null,同樣網路到處是解決這個問題的所謂解決方案。

網上有人說,一開始location是很有可能是null的 ,這是因為程式還從來沒有請求 過,只需重新請求更新location,並註冊***以接收更新後的location資訊。

locationlistener locationlistener = new locationlistener() 

@override

public

void onproviderenabled(string provider)

@override

public

void onproviderdisabled(string provider)

@override

public

void onlocationchanged(location location)

};mlocationmanager.requestlocationupdates(serviceprovider, 10000, 1, this);

然後你發現onlocationchanged永遠不會被呼叫,你仍然沒法獲取定位資訊。

3.  修改androidmanifest檔案,在該檔案裡新增如下配置

android:name="android.permission.access_fine_location" />上面meta-data中value的值改為你自己的金鑰

public

class

locationutil

return minstance;

}context mcontext;

string mprovider;

public bdlocationlistener mylistener = new mylocationlistener();

private locationclient mlocationclient;

public locationutil(context context)

public

void startmonitor()

if (mlocationclient != null && mlocationclient.isstarted()) else

}public

void stopmonitor()

}public bdlocation getlocation()

public mlocation getbaselocation()

private

void initparams()

public

class

mylocationlistener

implements

bdlocationlistener

mlocation = location;

mbaselocation.latitude = mlocation.getlatitude();

mbaselocation.longitude = mlocation.getlongitude();

stringbuffer sb = new stringbuffer(256);

if (location.getloctype() == bdlocation.typegpslocation) else

if (location.getloctype() == bdlocation.typenetworklocation)

if (debug) log.d(tag, "" + sb);

}public

void onreceivepoi(bdlocation poilocation)

}public

class

mlocation

}

**:

筆記 安卓 使用breakpad定位崩潰問題

極客時間 參考參考 參考專案 編譯之後執行崩潰生成dump檔案 android studio安裝目錄下 有個minidump stackwalk.exe d android studio install dir bin lldb bin 使用minidump stackwalk.exe dump c...

安卓版本問題

本文 分類 學習筆記 2012 05 30 08 17 6689人閱讀收藏 舉報android api平台 integer attributes google 語法 syntax android minsdkversion integer android targetsdkversion integ...

安卓學習問題彙總

1.textview.settext string text text必須為string,如果傳入int,不會報編譯錯誤,但是會報執行時異常。2.獲取乙個view的幾種方法 a 傳入int context.findviewbyid int b 傳入string 通過反射獲取 c 給view設定tag...