【问题标题】:Find city name from my location从我的位置查找城市名称
【发布时间】:2014-03-02 18:54:20
【问题描述】:

有没有办法从我当前的位置(Android)获取城市名称?我知道如何获得纬度/经度,但如果有一个具体的例子那就更好了

谢谢

【问题讨论】:

标签: android geolocation


【解决方案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】:

    只需使用谷歌的 API

    http://maps.googleapis.com/maps/api/geocode/json?latlng=&lt;LAT&gt;,&lt;LNG&gt;&amp;sensor=true

    把你的经纬度放在合适的地方,用一个JSONObject来解析结果

    【讨论】:

      【解决方案3】:

      试试这个,希望对你有帮助。对不起,循环,刚刚从我最近的工作中取出。

      看看这个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();
      }
      

      【讨论】:

        【解决方案4】:

        也许这篇文章可以帮助你: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 获取完整代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-21
          • 1970-01-01
          • 1970-01-01
          • 2016-07-29
          • 1970-01-01
          • 2010-09-20
          相关资源
          最近更新 更多