从邮编中获取经纬度 - Google Maps API

81

我所需的只是一些简单的示例代码,以显示如何从输入的邮政编码或城市/州获取latlng元素。


1
你是否需要指定一个编程语言作为示例? - Mark
8个回答

108

你能否直接调用以下代码,将 {zipcode} 替换为邮政编码或城市和州的名称? http://maps.googleapis.com/maps/api/geocode/json?address={zipcode}

Google 地理编码

这里有一个使用 JavaScript 进行地理编码的操作步骤链接:地理编码演示。如果你需要特定的纬度和经度,请调用 geometry.location.lat() 或 geometry.location.lng() (google.maps.LatLng 类的 API)。

以下是获取纬度和经度的示例:

    var lat = '';
    var lng = '';
    var address = {zipcode} or {city and state};
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
         lat = results[0].geometry.location.lat();
         lng = results[0].geometry.location.lng();
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
    alert('Latitude: ' + lat + ' Logitude: ' + lng);

4
这并不是错误的,但有时候会出现谷歌地图网站可以找到而此方法不能找到的情况。我发现一个解决方法:用这种方式替换它 { 'address': zipcode },变成这样 { 'address': 'zipcode ' + zipcode },我只是在邮政编码前面添加了字符串“zipcode”,这样就更有效了。 - Geovanny Buitrago

20

小提示: 邮政编码并非全球唯一,因此在请求中提供国家ISO代码很重要 (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)。

例如,在寻找波兰 (ISO代码为PL) 邮编01-210的坐标:

https://maps.googleapis.com/maps/api/geocode/json?address=01210,PL

如何获得用户所在的国家代码?

如果您想根据IP地址获取用户所在国家的信息,则有相应的服务,例如可对以下网址进行GET请求: http://ip-api.com/json


1
谢谢提示,仅使用地址=01210并没有得到正确的结果。添加“,PL”后可以正常工作! - Havrin

8
这里是从邮政编码获取经纬度的最可靠方法:
https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&components=postal_code:97403

3
此API不支持使用有“referer限制”的API密钥。 - fdrv

5
在实习项目中,我发现了一个与此相关的网站https://thezipcodes.com/。创建免费帐户并从帐户部分获取API密钥。
https://thezipcodes.com/api/v1/search?zipCode={zipCode}&countryCode={2digitCountryCode}&apiKey={apiKey}

我在这里找到了大部分数据。


5

这只是以前答案的改进,因为它在某些邮政编码上对我不起作用,即使在https://www.google.com/maps上也是如此,我只需在放置邮政编码之前添加单词“zipcode”,就像这样:

function getLatLngByZipcode(zipcode) 
{
    var geocoder = new google.maps.Geocoder();
    var address = zipcode;
    geocoder.geocode({ 'address': 'zipcode '+address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();
            alert("Latitude: " + latitude + "\nLongitude: " + longitude);
        } else {
            alert("Request failed.")
        }
    });
    return [latitude, longitude];
}


这对于像12345这样默认提供街道地址结果的数字非常有帮助。谢谢。与国家代码后缀(,US)结合使用,它可以更好地假定提供的是地址而不是商业名称。很好的东西,只要你只在搜索一个国家。 - James Tomasino
无法正常工作,返回时会显示未定义。 - always-a-learner

2

这是我在工作中使用的函数:

function getLatLngByZipcode(zipcode) 
{
    var geocoder = new google.maps.Geocoder();
    var address = zipcode;
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();
            alert("Latitude: " + latitude + "\nLongitude: " + longitude);
        } else {
            alert("Request failed.")
        }
    });
    return [latitude, longitude];
}

1
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY"></script>
<script>
    var latitude = '';
    var longitude = '';

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode(
    { 
       componentRestrictions: { 
           country: 'IN', 
           postalCode: '744102'
       } 
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            latitude = results[0].geometry.location.lat();
            longitude = results[0].geometry.location.lng();
            console.log(latitude + ", " + longitude);
        } else {
            alert("Request failed.")
        }
    });
</script>

https://developers.google.com/maps/documentation/javascript/geocoding#ComponentFiltering


0
这是一种使用Python包从邮政编码中提取经纬度的方法!
import pgeocode

nomi = pgeocode.Nominatim('us')
query = nomi.query_postal_code("90001")

data = {
    "lat": query["latitude"],
    "lon": query["longitude"]
}

print(data)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接