如何在android中使用反向地理编码获取邮政编码

时间:2013-06-20 14:25:21

标签: android geocoding

我是Andriod开发的新手。我尝试使用以下代码从反向地理编码中获取邮政编码和城市名称。找到纬度,经度值很好但无法获得城市名称和邮政编码。

package com.example.code;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LbsGeocodingActivity extends Activity {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

protected LocationManager locationManager;

protected Button retrieveLocationButton;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

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

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            MINIMUM_TIME_BETWEEN_UPDATES,
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
    );

retrieveLocationButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showCurrentLocation();
        }
});       

}   

protected void showCurrentLocation() {

    Location location =    locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Geocoder gcoder=new Geocoder(this, Locale.ENGLISH);
    List<Address> Data = null;
    double latitude;
    double longitude;
    if (location != null) {

        latitude=location.getLatitude();
        longitude=location.getLongitude();

        try {
            Data=gcoder.getFromLocation(latitude, longitude, 1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String cityname=Data.get(0).getLocality();
        String postalcode=Data.get(0).getPostalCode();

        String message=String.format("City Name \n PostalCode \n",  Data.get(0).getLocality(),Data.get(0).getPostalCode());
        Toast.makeText(LbsGeocodingActivity.this,message ,
                Toast.LENGTH_LONG).show();
    }

}  

private class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String s, int i, Bundle b) {
        Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String s) {
        Toast.makeText(LbsGeocodingActivity.this,
                "Provider disabled by the user. GPS turned off",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        Toast.makeText(LbsGeocodingActivity.this,
                "Provider enabled by the user. GPS turned on",
                Toast.LENGTH_LONG).show();          }

}

}

1 个答案:

答案 0 :(得分:0)

看起来您查找城市和邮政编码的代码是正确的,但是您为吐司格式化字符串的代码是错误的。它没有把值放在那里。

它应该是这样的:

String message=String.format("City Name %s \n PostalCode %d \n",  Data.get(0).getLocality(),Data.get(0).getPostalCode());

%s表示字符串,%s表示十进制数字(整数)。