从经度和纬度google api获取邮政编码

时间:2012-05-09 21:38:40

标签: javascript jquery ajax google-maps

我只需要知道如何从经度和纬度获取邮政编码。

我正在使用google maps api来获取基于搜索的long和lat,例如'manchester',然后我需要从中获取邮政编码来查询我的数据库。

到目前为止,我的代码是

function getLatLng()
{
    var localSearch = new google.maps.Geocoder();
    var postcode = $("#address").val();
    localSearch.geocode({ 'address': postcode },
                    function(results, status) {
                        if (results.length == 1) {
                            var result = results[0];
                            var location = result.geometry.location;
                        }
                        else 
                        {
                            $("#error").html("Address not found");
                        }
                    });
}

3 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

  

..搜索如'曼彻斯特'我需要一个来获取邮政编码..

您可能需要考虑: -

世界上有不止一个'曼彻斯特'。

许多邮政编码覆盖了英国的'曼彻斯特';

,如果查询过于笼统,地理编码可能只提供其中一种,或者不提供任何一种。

显然,由于邮局许可问题,返回的英国邮政编码可能是零碎的 - 只返回最重要的部分。

答案 2 :(得分:0)

首先,您应该知道,当您搜索“曼彻斯特”时,您可以获得邮政编码以及纬度和经度。请查看this part of the docs。查看the reply you get from google,您就可以从中获取邮政编码。

或者如果你想运行另一个查询从你拥有的坐标获取邮政编码(这似乎没用,因为我说你可以在一次调用API时这样做),你可以做Geert的反向地理编码-Jan建议。