[英]Android - What is the correct way of using this GPS Tracker function?
我想獲得經度和緯度。 我在MainActivity中有此功能
private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}
但是,當我使用它作為
Toast.makeText(this, "" + getGPS() + "", Toast.LENGTH_SHORT).show();
在onCreate中,它給我像DE [R @ F]這樣的奇怪字符。 使用我的函數正確獲取經度和緯度的正確方法是什么?
在onCreate中,它給我像DE [R @ F
您正在打印double[]
的對象標識符。
使用我的函數正確獲取經度和緯度的正確方法是什么?
分別訪問數組的兩個元素。
總是返回0,0
getLastKnownLocation()
通常返回null
。 僅將getLastKnownLocation()
用作優化 。 使用requestLocationUpdates()
請求設備嘗試找到該設備。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.