好的密碼破譯,這裏是我如何實現我的位置提取程序:
// the listener to listen to the locations
private LocationListener listener = null;
// a location manager
private LocationManager lm = null;
// locations instances to GPS and NETWORk
private Location myLocationGPS, myLocationNetwork;
// instantiates fields
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
myLocationNetwork = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
myLocationGPS = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
listener = new myLocationListener();
// the listener that gonna notify the activity about location changes
public class myLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// "location" is the RECEIVED locations and its here that you should proccess it
// check if the incoming position has been received from GPS or network
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
lm.removeUpdates(this);
} else {
lm.removeUpdates(listener);
}
}
@Override
public void onProviderDisabled(String provider) {
lm.removeUpdates(this);
lm.removeUpdates(listener);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
正如你所看到的,有沒有這麼很多技巧。在您實例化偵聽器時,您應該會看到Android設備頂部的GPS圖標。此外,只要您的位置發生變化(即隨着您的設備走動),OnLocationChanged方法將被調用。
另外,有趣的是,如果你只想得到你的位置,有幾種方法可以做到這一點,然後以不同的回報速度和不同的疾病。請檢查GoogleGLM(請求http://www.google.com/glm/mmap,返回json編碼與您的位置一致)服務,三角測量和網絡位置。在上面的片段中,我展示瞭如何通過GPS和網絡獲取位置。希望它有一些幫助... :)
我沒有得到你的問題......你想通過GPS獲取用戶的當前位置?如果是,那麼它的「非常簡單」:使一個類從LocationListener擴展並覆蓋onLocationChanged方法。您是否已將您的MD5 API密鑰用於Google地圖? – mthama
@mthama是和是..:D Infact這裏是我的[原問題](http://stackoverflow.com/questions/14692202/unable-to-get-current-gps-location-co-ordinates)。我已經完成了演習:D keystore..console..api key.pftt:p我缺少某處某處..或很可能..my電話被竊聽:但是,在這個世界怎麼樣?我的手機的谷歌地圖應用程序可以鎖定到GPS衛星上嗎? :哦,順便說一句,你是否嘗試過在HTC渴望上獲得GPS鎖?或者在基於android 2.3.3的設備上播放它? :| (實時..沒有DDMS thingy) – beerBear
你的代碼是複雜的,看看有什麼不正確的。我只能說你可以使用Criteria,也可以在沒有它的情況下請求GPS_PROVIDER。而「getMyLocation」是不好的類名。 Java中的類名應以大寫字母開頭,且不能是動詞。getMyLocation是一個很好的方法名,但是類應該被命名爲LocationRetreiver或者其他東西:)並且記住要獲得一個你可能希望在外面的GPS解析修復,或者更好地使用FakeGPS https://play.google.com/store/ apps/details?id = com.lexa.fakegps - 如果一切正常,你應該馬上得到定位。 –