2017-08-06 79 views
0

想要獲得經濟和戰略地位。我在MainActivityAndroid - 使用此GPS追蹤器功能的正確方法是什麼?

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。使用我的功能正確地獲得經度和姿態的正確方法是什麼?

+0

也許'Arrays.toString(getGPS())'? – MichalH

+0

@mike始終返回0,0 – user198989

回答

1

在的onCreate,這讓我奇怪的字符,如DE [R @頻率

要打印double[]的對象標識符。

什麼是使用我的函數正確獲取經度和姿態的正確方法?

單獨訪問數組的兩個元素。

回報總是0,0

getLastKnownLocation()通常返回null。僅使用getLastKnownLocation()作爲優化。使用requestLocationUpdates()來請求設備嘗試定位設備。

相關問題