【问题标题】:Get LatLng from Zip Code - Google Maps API从邮政编码获取 LatLng - Google Maps API
【发布时间】:2011-07-31 22:53:09
【问题描述】:

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

【问题讨论】:

  • 您在寻找您的示例时是否有特定的语言?

标签: javascript google-maps-api-3


【解决方案1】:

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

Google Geocoding

这是一个如何使用 JavaScript 进行地理编码的链接:Geocode walk-thru。如果您需要特定的纬度/经度数字,请调用 geometry.location.lat() 或 geometry.location.lng() (API for google.maps.LatLng class)

获取 lat/lng 的示例:

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

【讨论】:

  • 这没有错,但有时即使谷歌地图网站也无法到达某些地方,对我有用的方式是:而不是这个 { 'address': zipcode} 我替换成这样 { 'address': 'zipcode'+zipcode },我只是在邮政编码之前添加了字符串'zipcode',效果更好。
【解决方案2】:

提示:邮政编码并非全球唯一,因此值得在请求中提供国家/地区 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

【讨论】:

  • 感谢您的提示,仅使用 address=01210 并没有提供正确的结果。加上“,PL”就可以了!
【解决方案3】:

这是从邮政编码(即邮政编码)获取纬度/经度的最可靠方法:

https://maps.googleapis.com/maps/api/geocode/json?key=YOUR_API_KEY&components=postal_code:97403

【讨论】:

  • 具有引用者限制的 API 密钥不能与此 API 一起使用。
【解决方案4】:

这只是对先前答案的改进,因为即使在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),它可以更好地假设提供的是地址,而不是公司名称。好东西,只要您只搜索一个国家/地区。
  • 不起作用,它将显示未定义
【解决方案5】:

在从事我的实习项目时,我找到了一个网站https://thezipcodes.com/ 创建一个免费帐户并从帐户部分获取 API 密钥。

https://thezipcodes.com/api/v1/search?zipCode={zipCode}&countryCode={2digitCountryCode}&apiKey={apiKey}

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

【讨论】:

    【解决方案6】:

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

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

    【讨论】:

      【解决方案7】:
      <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

      【讨论】:

        猜你喜欢
        • 2011-11-28
        • 2021-06-08
        • 2017-02-15
        • 2018-01-03
        • 2013-06-21
        • 2016-03-13
        • 2013-04-08
        • 2012-10-25
        相关资源
        最近更新 更多