【问题标题】:How to use zip code instead Lat and Lng in Google maps API如何在 Google 地图 API 中使用邮政编码代替 Lat 和 Lng
【发布时间】:2012-06-21 22:33:51
【问题描述】:

我想知道是否有办法使用 zipcode 代替 Lat 和 Lng,使用这个 sn-p 或使用 Geocoding Requests。

    <script type="text/javascript">
        function initialize() {
            var latlng = new google.maps.LatLng(22.1482635,-100.9100755);
            // var latlang = new google.maps.zipcode(7845); <- Is there a way to do that?
            var myOptions = {
                zoom: 8,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            var marker = new google.maps.Marker({
                position: latlng,
                map: map,
                title:"zipcode"
            });

        }
    </script>

【问题讨论】:

    标签: google-maps maps zipcode


    【解决方案1】:

    您可以使用 Google GeoCode API 请求邮政编码的纬度和经度,然后从那里开始。

    如果您想直接或通过其他媒介执行此操作,API 请求 URL 将如下所示

    http://maps.googleapis.com/maps/api/geocode/json?address=50323&sensor=false

    50323 是您的邮政编码。

    或者您可以使用 google Javascript API 通过 jQuery 来完成这一切。

    var geocoder; //To use later
    var map; //Your map
    function initialize() {
      geocoder = new google.maps.Geocoder();
      //Default setup
      var latlng = new google.maps.LatLng(-34.397, 150.644);
      var myOptions = {
        zoom: 8,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }
    
    //Call this wherever needed to actually handle the display
    function codeAddress(zipCode) {
        geocoder.geocode( { 'address': zipCode}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            //Got result, center the map and put it out there
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
          } else {
            alert("Geocode was not successful for the following reason: " + status);
          }
        });
      }
    

    【讨论】:

    • 给出错误 Uncaught nf: in property address: not a string
    • 同样的问题,如果我将邮政编码作为字符串传递到南非(尝试 8790,来自比利时的邮政编码),如果我将邮政编码作为整数传递,则返回与 @ 相同的错误Mohd.Umar 说
    • 自从在这里发布一篇文章已经三年了,但我只是想提个醒。您希望 google 将其视为邮政编码。例如,maps.googleapis.com/maps/api/geocode/json?address=31158+usa,它将 31158 视为街道名称。如果我不添加美国,它会带我去法国
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 2018-11-02
    • 1970-01-01
    相关资源
    最近更新 更多