简体   繁体   English

Google Maps API v3标记

[英]Google maps API v3 markers

Can any one give me an idea on how to zoom your map according to the markers. 谁能给我一个关于如何根据标记缩放地图的想法。 Actually I have geocoded the addresses and the markers are shown on the map. 实际上,我已经对地址进行了地理编码,并且标记显示在地图上。 But they are not centered ie I have to drag the map to see the other marker.I want my map to fit bounds using the addresses that I have in my addresses array. 但是它们没有居中,即我必须拖动地图才能看到其他标记。我希望我的地图能够使用我的addresses数组中的地址来适合边界。 How this can be done.? 如何做到这一点? My current code for geocoding is: 我当前的地址解析代码是:

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

In your function where you are creating the markers you can also extend the bounds (with 在创建标记的函数中,您还可以扩展范围(使用

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

If you want to set a maximum zoom level no matter what the actual bounds are you can add this after the map.fitBounds (you can change the level of zoom): 如果要设置最大缩放级别,无论实际范围是多少,都可以在map.fitBounds之后添加(可以更改缩放级别):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM