ZipCode from location

后端 未结 3 535
野趣味
野趣味 2020-12-06 08:13

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

相关标签:
3条回答
  • 2020-12-06 08:17

    Use Android Geocoder API!

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

    0 讨论(0)
  • 2020-12-06 08:27

    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.

    0 讨论(0)
  • 2020-12-06 08:29

    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

    0 讨论(0)
提交回复
热议问题