2014-01-08 36 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再次工作(在突然停止工作之后)的错误。我会使用谷歌网络服务的 –