缩小地理编码器地址仅限于城市

时间:2013-07-10 19:46:26

标签: android reverse-geocoding

我有一个地理编码器,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 个答案:

答案 0 :(得分:5)

很高兴您自己找到了解决方案。

您可能希望使用下面的方法,这更简单,因为它使用Location 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);

即使这段代码is supplied by another user,我自己已经实现了这种精确的方法,并发现它很有用。

希望这有帮助。

答案 1 :(得分:1)

我自己找到了解决方案,并且我正在分享它以供将来参考其他有需要的人使用。

定义地址后,我需要做的就是为城市名称提供address.getLocality()。

对于这个例子,它将是

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

答案 2 :(得分: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

    }

}