2011-08-03 290 views
15

我有什么:目前我的应用只告诉我我当前位置的坐标。从取坐标获取位置名称

我想要什么:从GPS获取的坐标获取位置名称,以便我可以知道我到底在哪里。 (位置名称)

+1

你在找这个“中给出-一个纬度和经度,得到最位置的名称] [1 ]” [1]:http://stackoverflow.com/questions/6172451/given-a-latitude-and-longitude-get-the-location-name – Samuel

+0

看一看[如何编程谷歌Android](http://blogoscoped.com/archive/2008-12-15-n14.html) –

+1

你的问题是相同的问题,我发布了几个月back..check此链接http:///stackoverflow.com/questions/6172451/given-a-latitude-and-longitude-get-the-location-name或检查我的答案下面... – Krishna

回答

27

这里是获取长期完整的代码 - 纬度,以获得地址:

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
String provider = locationManager.getBestProvider(new Criteria(), true); 

Location locations = locationManager.getLastKnownLocation(provider); 
List<String> providerList = locationManager.getAllProviders(); 
if(null!=locations && null!=providerList && providerList.size()>0){     
double longitude = locations.getLongitude(); 
double latitude = locations.getLatitude(); 
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());     
try { 
    List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1); 
    if(null!=listAddresses&&listAddresses.size()>0){ 
     String _Location = listAddresses.get(0).getAddressLine(0); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

} 
+2

有时这种反向地理编码技术没有给出任何地址。所以每当我需要时,如何从lat-long获得地址? –

+1

是的,它不会在某个时间返回地址。为此,您可以实施备份机制,例如,当您没有再次获取地址时获取地址,或者可以使用google api获取地址。 –

+0

使用此权限:<使用权限android:name =“android.permission.ACCESS_COARSE_LOCATION”/> <使用权限android:name =“android.permission.ACCESS_FINE_LOCATION”/> –

8

您可以将我们的GeoCoder在android.location中提供。 Geocoder包。 JavaDocs给你充分的解释。你可能的样本。

List<Address> list = geoCoder.getFromLocation(location 
       .getLatitude(), location.getLongitude(), 1); 
     if (list != null & list.size() > 0) { 
      Address address = list.get(0); 
      result = address.getLocality(); 
      return result; 

result将返回位置的名称。

6

在这里,我给了一个单一的只是通过经纬度在这个功能,然后你得到了所有与这个经度和纬度有关的信息。

public void getAddress(double lat, double lng) { 
    Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault()); 
    try { 
     List<Address> addresses = geocoder.getFromLocation(lat, lng, 1); 
     Address obj = addresses.get(0); 
     String add = obj.getAddressLine(0); 
     GUIStatics.currentAddress = obj.getSubAdminArea() + "," 
       + obj.getAdminArea(); 
     GUIStatics.latitude = obj.getLatitude(); 
     GUIStatics.longitude = obj.getLongitude(); 
     GUIStatics.currentCity= obj.getSubAdminArea(); 
     GUIStatics.currentState= obj.getAdminArea(); 
     add = add + "\n" + obj.getCountryName(); 
     add = add + "\n" + obj.getCountryCode(); 
     add = add + "\n" + obj.getAdminArea(); 
     add = add + "\n" + obj.getPostalCode(); 
     add = add + "\n" + obj.getSubAdminArea(); 
     add = add + "\n" + obj.getLocality(); 
     add = add + "\n" + obj.getSubThoroughfare(); 

     Log.v("IGA", "Address" + add); 
     // Toast.makeText(this, "Address=>" + add, 
     // Toast.LENGTH_SHORT).show(); 

     // TennisAppActivity.showDialog(add); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); 
    } 
} 

我希望你能解决你的答案。

+0

一个非常复杂的... – Noman

+0

我怎样才能得到我的地方的确切位置,它给出最大的城市价值,我想城市内的位置名称 – Apoorv