5

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

4

3 回答 3

8

使用 Android地理编码器 API

getFromLocation(double, double, int) 就是您所需要的。

于 2011-11-25T06:28:50.457 回答
5

是的,您可以...您需要做的就是提取

http://maps.google.com/maps/geo?ll=纬度、经度

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

或者尝试尝试这个解决方案。我可以给你一个想法。

你可以有一个城市吗?有了这个 json 数据,如果你有一个具有这种格式的邮政编码的数据库(在 maxmind.com)

国家代码 | 城市 | 邮政编码


PH |宿务|6000

现在查询与谷歌返回的国家代码相关的数据库。

更新

Geocoding API v2 已于 2013 年 9 月 9 日被拒绝。

这是 API 服务的新 URL

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

于 2011-05-02T10:07:41.463 回答
4

基于地理编码器的实现:

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;
}

您还可以从地址获取其他信息,例如城市名称。

于 2014-06-17T06:46:08.553 回答