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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10524474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:42:41  来源:igfitidea点击:

Get postcode from longitude and latitude google api

javascriptjqueryajaxgoogle-maps

提问by lnelson92

I just need to know how to go about getting a postcode from the longitude and latitude.

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

Im using the google maps api to get the long and lat based on a search such as 'manchester' i then need a to get the postcode from this to query my database.

我使用谷歌地图 api 根据诸如“曼彻斯特”之类的搜索来获取经纬度,然后我需要从中获取邮政编码以查询我的数据库。

My code so far is

到目前为止我的代码是

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

回答by rossko57

.. search such as 'manchester' i then need a to get the postcode ..

.. 搜索诸如“曼彻斯特”之类的,然后我需要一个来获取邮政编码 ..

You might need to think about:-

您可能需要考虑:-

There is more than one 'manchester' in the world.

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

'Manchester' in the UK is covered by many postcodes; geocoding may give you only one of them, or none of them if the query is too general.

英国的“曼彻斯特”被许多邮政编码覆盖;地理编码可能只给你其中之一,如果查询太笼统,则不给你一个。

Apparently due to Post Office licencing issues, returned UK postcodes may be fragmentary - only the most significant part is returned.

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

回答by aniri

First of all, you should know that you can get the postcode together with latitude and longitude when you search for 'manchester'. Check out this part of the docsfor this. Check out the reply you get from googleand you can get the postcode from that.

首先,您应该知道,当您搜索“manchester”时,您可以将邮政编码与经纬度一起获取。为此,请查看文档的这一部分。查看你从谷歌得到的回复,你可以从中获得邮政编码。

Or if you want to run another query to get the postcode from the coordinates you have (which doesn't seem useful since as I said you can do this in one call to the API), you can do reverse geocoding as Geert-Jan suggested.

或者,如果您想运行另一个查询以从您拥有的坐标中获取邮政编码(这似乎没有用,因为正如我所说,您可以在一次调用 API 中完成此操作),您可以按照 Geert-Jan 的建议进行反向地理编码.