地理编码器地址缩小到仅城市

3
我有一个地理编码器gcd,下面这行代码用于反向地理编码:
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
                    if(addresses != null) {
                        Address returnedAddress = addresses.get(0);

                        for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                       strReturnedAddress = (returnedAddress.getAddressLine(i)).toString();

strReturnedAddress返回类似于

10453 Central Bronx, New York City, NY

我只需要城市名称,即纽约市。

由于地理编码的输出可能会改变,删除字符串的某些部分会非常困难。我只需要地理编码提供给我城市信息。

我已经查看了http://developer.android.com/reference/android/location/Geocoder.html,但没有找到答案。

3个回答

5

很棒,你自己找到了解决方案。

不过,你可以用下面的方法,这个方法更简单,因为它利用了位置API,而不需要遍历地址列表:

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);

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

尽管这段代码由另一个用户提供, 但我自己实现了这个方法并发现它很有用。
希望这可以帮到你。

没关系,兄弟。我觉得这样做会让你的工作更简单。 - Demitrian

1

我已经找到了解决方案,现在分享给其他需要的人作为参考。

当地址被定义后,我只需要使用address.getLocality()获取城市名称。

对于这个实例,代码如下:

Address returnedAddress = addresses.get(0);
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                       strReturnedAddress = (returnedAddress.getLocality().toString();

0
public class Locationfinder extends Activity implements LocationListener{   
     private TextView latituteField,longitudeField, Address;
      private LocationManager locationManager;
      private String provider;
    List<Address> mAddresses;
       double lat,lng;
       public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            latituteField = (TextView) findViewById(R.id.TextView02);
            longitudeField = (TextView) findViewById(R.id.TextView04);
            Address=(TextView)findViewById(R.id.TextView03);

            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            Criteria criteria = new Criteria();
            provider = locationManager.getBestProvider(criteria, false);
            Location location = locationManager.getLastKnownLocation(provider);

            // Initialize the location fields
            if (location != null) {

              onLocationChanged(location);
            } else {
              latituteField.setText("Location not available");
              longitudeField.setText("Location not available");
            }
       }

       protected void onResume() {
            super.onResume();
            locationManager.requestLocationUpdates(provider, 400, 1, this);
          }

//         Remove the locationlistener updates when Activity is paused 
          @Override
          protected void onPause() {
            super.onPause();
            locationManager.removeUpdates(this);
          }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        lat = (double) (location.getLatitude());
         lng = (double) (location.getLongitude());
        System.out.println("lat1: " + lat +"    " +"lng1" +lng);
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));

       Geocoder gcd = new Geocoder(getApplicationContext(),
               Locale.getDefault());
       try {


                 mAddresses = gcd.getFromLocation(lat,lng, 1);

           String address = mAddresses.get(0).getAddressLine(0);
              String city = mAddresses.get(0).getAddressLine(1);
              String country = mAddresses.get(0).getAddressLine(2);

              Address.setText("Address:- " + address + "city :" +city + "country : "+ country);


       } catch (IOException e) {
         e.printStackTrace();
         latituteField.setText("Location not available");
          longitudeField.setText("Location not available");
       }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Disable GPS " + provider,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Enable GPS " + provider,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

GPS定位器,用于查找当前地址.. 代码写得真不错 :) - elamathy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接