5

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

razlebe
  • 7,134
  • 6
  • 42
  • 57
Uday
  • 5,933
  • 9
  • 44
  • 76

3 Answers3

8

Use Android Geocoder API!

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

lordhong
  • 1,227
  • 1
  • 12
  • 19
5

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

spicykimchi
  • 1,151
  • 5
  • 22
  • 41
  • @Udaykiran you need another database that has a record of a zipcode.This is the example of the database.http://ipinfodb.com/zipcode_database.php – spicykimchi May 02 '11 at 10:25
  • If you want to download also another zipcode database,Here ZipCode Database – spicykimchi May 02 '11 at 10:35
  • Thanks u just saved my day!!!! and @Udaykiran try to give lat long of USA, it will give you the postal code, i don't know what is the problem for India!!!! – Shrikant Sep 15 '12 at 10:34
  • 1
    This answer is no longer valid since the Geocoding API v2 has been turned down by Google. Android Geocode API is a better solution. – Christer Nordvik Nov 27 '13 at 22:09
  • how can ZIP code using lat, "long..http://maps.googleapis.com/maps/api/geocode/json?latlng=your_latitude,your_longitude" also not respond zip code...any suggestion? – CoDe Mar 18 '14 at 11:03
  • FYI Zip code data is not static...it's a bad recommendation to use a static database. – stevebot Dec 17 '14 at 18:50
4

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.

Alex Kucherenko
  • 20,168
  • 2
  • 26
  • 33