从我的位置查找城市名称

时间:2014-03-02 18:54:21

标签: android geolocation

有没有办法从当前位置(Android)获取城市名称?我知道如何获得lattitue / long但是如果有一个具体的例子则更好

谢谢

4 个答案:

答案 0 :(得分:1)

只需使用谷歌的API

http://maps.googleapis.com/maps/api/geocode/json?latlng=<LAT>,<LNG>&sensor=true

将您的纬度和经度放在正确的位置,并使用JSONObject来解析结果

答案 1 :(得分:1)

     Geocoder gcd = new Geocoder(context, Locale.getDefault());
     try{
     List<Address> addresses = gcd.getFromLocation(lat, lon, 1);//lat and lon is the latitude and longitude in double
     if (addresses.size() > 0) 
     String cityName=addresses.get(0).getLocality();
     }
     catch (IOException e) {}
     catch (NullPointerException e) {}

答案 2 :(得分:0)

试试这个,希望它有所帮助。请原谅,我刚刚从我最近的一份作品中把它拿出来。

看看这个link

Geocoder geocoder = new Geocoder(getApplicationContext(),Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude,longitude,1);
for(int i=0; i<addresses.size(); ++i)
{
    address = addresses.get(i);
     string city = address.getLocality();
}

答案 3 :(得分:0)

这篇文章可以帮助你:http://www.devlper.com/2010/07/geocoding-and-reverse-geocoding-in-android/ 它还提供了源代码。

另外,来自here

public class AndroidFromLocation extends Activity {

double LATITUDE = 37.42233;
double LONGITUDE = -122.083;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
   TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
   TextView myAddress = (TextView)findViewById(R.id.myaddress);

   myLatitude.setText("Latitude: " + String.valueOf(LATITUDE));
   myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE));

   Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

   try {
   List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

   if(addresses != null) {
   Address returnedAddress = addresses.get(0);
   StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
   for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
   strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
  }
 myAddress.setText(strReturnedAddress.toString());
}
else{
myAddress.setText("No Address returned!");
}
} catch (IOException e) {
 // TODO Auto-generated catch block
  e.printStackTrace();
 myAddress.setText("Canont get Address!");
 }

 }
 }

另外,请查看此具体示例:http://www.smnirven.com/blog/2009/10/09/reverse-geocode-lookup-using-google-maps-api-on-android-revisited/并从here获取完整代码。