将多个英国邮政编码列表添加到谷歌地图

Add multiple UK Postcodes list to Google Maps

本文关键字:添加 谷歌地图 列表 邮政编码 英国      更新时间:2023-09-26

我已经尝试了大量的组合,并阅读了Stackoverflow上的大量文章,但似乎无法做到这一点。 应该很容易,但事实证明是背面的正确痛苦,所以只是想知道是否有人可以提供帮助..

只是试图在谷歌地图(以及最终的方向)中添加一些标记,我有下面的代码,但我得到的只是空白屏幕。如果我去掉标记代码,我确实会得到裸图,所以它至少在页面中工作...... 谁能帮忙..?

<script type="text/javascript">
//<![CDATA[
                var geocoder;
                var map;
                function initialize() {
                  geocoder = new google.maps.Geocoder();
                  var mapOptions = {
                    zoom: 7
                  }
                  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
                  codeAddress();
                }
                function codeAddress() {
                  var address = ['SO21 3NE','Horsham'];
                  geocoder.geocode( { 'address': address}, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                      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);
                    }
                  });
                }
                google.maps.event.addDomListener(window, 'load', initialize);//]]>
 </script>

这修复了它...

 var locations = ['SO21 3NE','Horsham','RG45 7EG'];
                var infowindow = new google.maps.InfoWindow();
                var marker, i;
                var geocoder = new google.maps.Geocoder();
                var mapOptions = {
                    zoom: 7
                  }
                map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
                for (i = 0; i < locations.length; i++) {
                geocoder.geocode( { 'address': locations[i]}, function(results, status) {
                    //alert(status);
                    if (status == google.maps.GeocoderStatus.OK) {
                        //alert(results[0].geometry.location);
                        map.setCenter(results[0].geometry.location);
                        marker = new google.maps.Marker({
                            position: results[0].geometry.location,
                            map: map
                        }); 
                        google.maps.event.addListener(marker, 'mouseover', function() { infowindow.open(map, marker);});
                        google.maps.event.addListener(marker, 'mouseout', function() { infowindow.close();});
                    }
                    else
                    {
                        alert("some problem in geocode" + status);
        }
    }); 
}
                 google.maps.event.addDomListener(window, 'load', initialize);

我的代码出现javascript错误:Uncaught InvalidValueError: in property address: not a string

地理编码器请求的地址属性是数组而不是字符串。

function codeAddress() {
  var address = ['SO21 3NE','Horsham'];
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      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);
    }
  });
}

应该是:

function codeAddress() {
  var address = 'SO21 3NE, Horsham';
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      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);
    }
  });
}

工作小提琴

此问题中有 25 个地址的示例