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, 纽约市, NY

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

由于地理编码的输出可能会发生变化,因此删除部分字符串将非常困难。我只需要地理编码给我城市。

我检查了http://developer.android.com/reference/android/location/Geocoder.html但找不到答案。

4

3 回答 3

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);

尽管此代码是由另一个用户提供的,但我自己已经实现了这种精确的方法,并且发现它很有用。

希望这可以帮助。

于 2013-07-10T21:27:39.873 回答
1

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

定义地址后,我需要做的就是使用 address.getLocality() 获取城市名称。

对于这种情况,它将是

Address returnedAddress = addresses.get(0);
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                       strReturnedAddress = (returnedAddress.getLocality().toString();
于 2013-07-10T21:19:04.260 回答
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

    }

}
于 2013-09-25T10:27:02.863 回答