简体   繁体   English

Google API v3适合地图缩放并限制所有标记

[英]Google API v3 fit map zoom and bounds to all markers

I have been working on this for a while and am trying to implement latlngbounds to fit all the pins in the map canvas dynamically. 我已经为此工作了一段时间,并尝试实现latlngbounds以动态地适应地图画布中的所有图钉。 But its not working. 但是它不起作用。 If someone has done sth similar to my code, can anyone guide me in the right path? 如果有人做了与我的代码相似的事情,那么有人可以引导我走正确的道路吗?

var geocoder; 
var map; 
function initializeMap() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(39.88445,-86.11084); 
    var myOptions = { 
      zoom: 9, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById("map_canvas"), 
        myOptions); 
} 
function codeAddress() { 
    var bound = new google.maps.LatLngBounds(); 
    var markers = new Array(); 
    var infowindow = new google.maps.InfoWindow({});  
    $('.LocAddHidden').each(function(index) { 
        var addy = $(this).text(); 
        geocoder.geocode( { 'address': addy}, function(results, status) { 
            if (status == google.maps.GeocoderStatus.OK)  
            { 
                map.setCenter(results[0].geometry.location); 
                var marker = new google.maps.Marker({ 
                position: results[0].geometry.location, 
                map: map,                
                title:addy       
                });      
            }  
        }); 
    }); 
} 

  $(document).ready(function(){ 
    initializeMap(); 
    codeAddress();    
  for(var i in markers) 
  { 
    bound.extend(markers[i].getPosition()); 
  } 
  map.fitBounds(bound); 
});                           

For IE8 to work.. 为IE8工作。

Instead of using: for(var i in markersArray) 而不是使用: for(var i in markersArray)

Use this: for (i = 0; i < markersArray.length; i++) 使用此命令: for (i = 0; i < markersArray.length; i++)

The main problem is that you're extending the bounds in an empty loop. 主要问题是您要在空循环中扩展边界。 I don't see where you're adding anything to the markers array you create at the start of codeAddress(). 我看不到要在codeAddress()开头创建的标记数组中添加任何内容的位置。 You should be adding the marker you create in the geocode request. 您应该在地理编码请求中添加创建的标记。

But instead of adding it to a markers array in one loop, then extending the bounds in another loop, you could get rid of the 2nd loop and just extend the bounds in the first loop. 但是,与其在一个循环中将其添加到markers数组中,然后在另一个循环中扩展边界,不如摆脱第二个循环,而仅在第一个循环中扩展边界。

And you don't need to recenter the map every loop iteration. 而且您不需要在每次循环迭代时都重新映射地图。 Setting the bounds at the very end takes care of recentering and zooming. 将边界设置在最末端会照顾到居中和缩放。

And Cheery pointed out another problem about variable scope. Cheery指出了有关可变范围的另一个问题。

After several attempts, I have something simple and practical: 经过几次尝试,我得到了一些简单实用的信息:

function simulateBounds() {

    var bounds = new google.maps.LatLngBounds;

    for (var i = 0; i < arrayChinchetas.length; i++) {
        bounds.extend(arrayChinchetas[i].getPosition());
    }

    SurOeste = bounds.getSouthWest();
    NorEste = bounds.getNorthEast();

    //Constructs a rectangle from the points at its south-west and north-east corners.
    var latlandBounds = new google.maps.LatLngBounds(SurOeste, NorEste);

    map.fitBounds(latlandBounds);

}

You'll need to declare the markers array in your code, and push each created marker into that array 您需要在代码中声明markers数组,并将每个创建的marker推入该数组

// global
var markers = [];

// after created
markers.push(marker);

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

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