2014-01-08 32 views

回答

4

這裏是位置的完整方法。 只要從任何地方打電話給我。 您將獲得用戶的當前位置。與滿街,城市和國家的地址

也可以傳遞經度和緯度值。 addresses = gcd.getFromLocation(latitude, longitude, 1);以獲取任何特定值的位置。

public String getUserLocation() { 

    String address = ""; 

    LocationManager locManager = (LocationManager) context 
      .getSystemService(Context.LOCATION_SERVICE); 

    boolean network_enabled = locManager 
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

    Location location; 

    if (network_enabled) { 

     location = locManager 
       .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

     if (location != null) { 
      double longitude = location.getLongitude(); 
      double latitude = location.getLatitude(); 

      Log.e("longitude = ", longitude + "lat"); 
      Log.e("latitude = ", latitude + "long"); 

      Geocoder gcd = new Geocoder(context, Locale.getDefault()); 
      List<Address> addresses = null; 
      try { 
       addresses = gcd.getFromLocation(latitude, longitude, 1); 

      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       return address; 
      } 
      if (addresses.size() > 0) { 

       String addressline = addresses.get(0).getAddressLine(0); 
       String city = addresses.get(0).getAddressLine(1); 
       String country = addresses.get(0).getAddressLine(2); 

       Log.e("Address", addressline + ", " + city + ", " + country); 
       return address = addressline + ", " + city + ", " + country; 
      } 

      return address; 
     } 
    } 

    return address; 

} 

希望這有助於:)

+1

哇哇哇哇哇哇哇哇哇哇哇哇哇哈哈:) –

+0

hal ray hal bishni ..:D – Ali

1

The Geocoder那樣做。儘管如此,它使用互聯網,但並不像你想要的那樣準確,但它完成了這項工作。

+0

前,我正在讀不同的職位幾個小時,他們說,地理編碼器不能在所有設備上工作。那是多麼真實? – Ramilol

+0

我第一次聽說它不適用於所有設備。它是SDK的一部分,不知道它是如何工作的。我曾經使用過它,儘管發現我的「位置」距離我去過的地方爲50/100m,但我認爲在美國它可能會更好(歐洲在這裏)。 我相信你可以使用[Google的方向API](https://developers.google.com/maps/documentation/directions/),但據我所知,這有一些限制。它適用於請求(因此,限制)。它返回由座標(如果需要)和地名(至少是街道)組成的JSON解析數據,但它是位置數據的另一個來源。 –

+0

P.S.有趣的是,你接受我給出的5分鐘後的地理編碼器答案...希望它至少適合你。 –

1

在地址對象,你有很多有趣的東西:

http://developer.android.com/reference/android/location/Address.html

getFeatureName()的爲例。

從緯度/經度座標檢索地址:

Geocoder geoCoder = new Geocoder(getApplicationContext(), Locale.getDefault()); 
List<Address> addresses = gcd.getFromLocation(lat, lng, 1); // the number of result you want, in your case probably just one. 
+0

正是我需要:)非常感謝你! – Ramilol

+0

請注意可能需要重新啓動才能使Geocoder再次工作(在突然停止工作之後)的錯誤。我會使用谷歌網絡服務的 –