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

时间:2014-12-23 18:49:24

标签: javascript google-maps google-maps-api-3

我已经尝试了大量的组合,并在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);

1 个答案:

答案 0 :(得分:0)

我得到javascript error with your codeUncaught InvalidValueError: in property address: not a string

GeocoderRequest的地址属性是一个数组而不是字符串。

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

working fiddle

来自example with 25 addresses

this question