ZipCode from location
Asked Answered
L

3

5

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

Lurdan answered 2/5, 2011 at 10:4 Comment(0)
M
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

Morris answered 2/5, 2011 at 10:7 Comment(6)
@Udaykiran you need another database that has a record of a zipcode.This is the example of the database.ipinfodb.com/zipcode_database.phpMorris
If you want to download also another zipcode database,Here <a href="populardata.com/downloads.html" >ZipCode Database</a>Morris
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!!!!Spragens
This answer is no longer valid since the Geocoding API v2 has been turned down by Google. Android Geocode API is a better solution.Thomism
how can ZIP code using lat, "long..maps.googleapis.com/maps/api/geocode/…" also not respond zip code...any suggestion?Kohlrabi
FYI Zip code data is not static...it's a bad recommendation to use a static database.Bedsore
T
8

Use Android Geocoder API!

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

Thebaine answered 25/11, 2011 at 6:28 Comment(0)
M
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

Morris answered 2/5, 2011 at 10:7 Comment(6)
@Udaykiran you need another database that has a record of a zipcode.This is the example of the database.ipinfodb.com/zipcode_database.phpMorris
If you want to download also another zipcode database,Here <a href="populardata.com/downloads.html" >ZipCode Database</a>Morris
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!!!!Spragens
This answer is no longer valid since the Geocoding API v2 has been turned down by Google. Android Geocode API is a better solution.Thomism
how can ZIP code using lat, "long..maps.googleapis.com/maps/api/geocode/…" also not respond zip code...any suggestion?Kohlrabi
FYI Zip code data is not static...it's a bad recommendation to use a static database.Bedsore
P
5

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.

Pariah answered 17/6, 2014 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.