Google map API v3标记

时间:2013-11-12 10:43:52

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

任何人都可以根据标记给我一个关于如何缩放地图的想法。实际上我已经对地址进行了地理编码,并且标记显示在地图上。但它们并不居中,即我必须拖动地图才能看到另一个标记。我希望我的地图使用我addresses数组中的地址来拟合边界。如何做到这一点。我目前的地理编码代码是:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var addresses = new Array();
abc = document.getElementsByTagName('td');
//loc = mydiv.getAttribute("data-addr");
var l = abc.length; 
 for (var i=0; i < l; i++){
    if (abc[i].hasAttribute('name'))
    {   
        addresses.push(""+abc[i].innerHTML+""); //removed single quotes here. see previous code
    }   
}
var len = addresses.length;
var geocoder;
var map;
var add = document.getElementById("addr").value;
window.onload = function init() {

      geocoder = new google.maps.Geocoder();    
      var add = document.getElementById("address").value;
      var latlng = codeAddress(add);
      var myOptions = {
        zoom: 10,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById("map_canvas"),
          myOptions);
}

//for (var i = 0; i < addresses.length; i++)
//{
    function codeAddress(add) 
    {
      //var addr = addresses[i];
      geocoder.geocode( { 'address':add }, function(results, status) {

          if (status == google.maps.GeocoderStatus.OK) {
             map.setCenter(results[0].geometry.location);     
          } else {
              alert("Geocode was not successful for the following reason: " + status);
          }
        });
    }

    function createMarkers()
    {
        for(var i = 0; i < len; i++){
            (function(addresses){
                geocoder.geocode( { 'address': addresses }, function(results) {
                    var marker = new google.maps.Marker ({
                        map: map,
                        position: results[0].geometry.location,//error:results[0] is undefined
                        title: address
                    }); 

                    google.maps.event.addListener(marker, 'click', function() {
                        alert(addresses);
                    });
                });
            })(addresses[i]);
        }
    }
    window.onload = createMarkers;
</script>

1 个答案:

答案 0 :(得分:1)

在您创建标记的函数中,您还可以扩展边界(使用

function createMarkers()
{
    //create the bounds for the map
    var bounds = new google.maps.LatLngBounds();

    for(var i = 0; i < len; i++){
        (function(addresses){
            geocoder.geocode( { 'address': addresses }, function(results) {
                var marker = new google.maps.Marker ({
                    map: map,
                    position: results[0].geometry.location,//error:results[0] is undefined
                    title: address
                }); 

                google.maps.event.addListener(marker, 'click', function() {
                    alert(addresses);
                });
            });
        })(addresses[i]);

        //extend the bounds with each address
        bounds.extend (addresses[i]);
    }
    //fit to the full list of bounds
    map.fitBounds(bounds); 
}

如果您想设置最大缩放级别,无论实际边界是什么,您都可以在map.fitBounds之后添加它(您可以更改缩放级别):

var listener = google.maps.event.addListener(map, "idle", function() { 
if (map.getZoom() > 10) map.setZoom(10); 
  google.maps.event.removeListener(listener); 
});