2010-07-16 33 views
2

我想檢索用戶當前位置的經度和緯度,我已經嘗試使用位置觀察者,該位置觀察者基於給定的輸入距離和時間觸發事件,因此它觸發事件爲根據給定的持續時間和距離識別位置。Abt檢索設備的經度和緯度

而我想要的是方法,只需通過調用該方法來獲取位置。當我使用這種方法時,它應該通知我經緯度。

在android中有任何方法嗎?

回答

2
/** 
* This is a fast code to get the last known location of the phone. If there 
* is no exact gps-information it falls back to the network-based location 
* info. This code is using LocationManager. Code from: 
* http://www.androidsnippets.org/snippets/21/ 
* 
* @param ctx 
* @return 
*/ 
public static Location getLocation(Context ctx) { 
    LocationManager lm = (LocationManager) ctx 
      .getSystemService(Context.LOCATION_SERVICE); 
    List<String> providers = lm.getProviders(true); 

    /* 
    * Loop over the array backwards, and if you get an accurate location, 
    * then break out the loop 
    */ 
    Location l = null; 

    for (int i = providers.size() - 1; i >= 0; i--) { 
     l = lm.getLastKnownLocation(providers.get(i)); 
     if (l != null) 
      break; 
    } 
    return l; 
} 
+0

謝謝,它很有用 – Rakesh 2010-07-16 11:23:10