ZipCode from location

2019-01-15 03:38发布

问题:

Is there any way to get zip-code of the current user location from location or lat or long. If so how?

回答1:

Yes you can...All you need to do is extract the

http://maps.google.com/maps/geo?ll=latitude,longitude

http://maps.google.com/maps/geo?ll=10.345561,123.896932

Or try experimenting this solutions.I can give you an idea.

You can have a city right? WIth this json data, If you have a database(at maxmind.com) of the zipcode with this format

CountryCode | City | ZipCode


PH |Cebu |6000

Now query your database that is relative to the countrycode that googles return.

UPDATE

The Geocoding API v2 has been turned down on September 9th, 2013.

Here's the new URL of API service

http://maps.googleapis.com/maps/api/geocode/json?latlng=your_latitude,your_longitude

http://maps.googleapis.com/maps/api/geocode/json?latlng=10.32,123.90&sensor=true



回答2:

Use Android Geocoder API!

getFromLocation(double, double, int) is all you need.



回答3:

Geocoder based implementation:

private String getZipCodeFromLocation(Location location) {
    Address addr = getAddressFromLocation(location);
    return addr.getPostalCode() == null ? "" : addr.getPostalCode();
}

private Address getAddressFromLocation(Location location) {
    Geocoder geocoder = new Geocoder(this);
    Address address = new Address(Locale.getDefault());
    try {
        List<Address> addr = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if (addr.size() > 0) {
            address = addr.get(0);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return address;
}

You can get also other information from address, for example city name.