2010-08-19 62 views

Respuesta

70

Como se sugirieron las otras respuestas, el método fitBounds() debería ser el truco.

Considere el siguiente ejemplo, que generará 10 puntos al azar en el noreste de EE.UU., y se aplica la fitBounds() método:

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps LatLngBounds.extend() Demo</title> 
    <script src="http://maps.google.com/maps/api/js?sensor=false" 
      type="text/javascript"></script> 
</head> 
<body> 
    <div id="map" style="width: 400px; height: 300px;"></div> 

    <script type="text/javascript"> 

    var map = new google.maps.Map(document.getElementById('map'), { 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
    }); 

    var markerBounds = new google.maps.LatLngBounds(); 

    var randomPoint, i; 

    for (i = 0; i < 10; i++) { 
    // Generate 10 random points within North East USA 
    randomPoint = new google.maps.LatLng(39.00 + (Math.random() - 0.5) * 20, 
              -77.00 + (Math.random() - 0.5) * 20); 

    // Draw a marker for each random point 
    new google.maps.Marker({ 
     position: randomPoint, 
     map: map 
    }); 

    // Extend markerBounds with each random point. 
    markerBounds.extend(randomPoint); 
    } 

    // At the end markerBounds will be the smallest bounding box to contain 
    // our 10 random points 

    // Finally we can call the Map.fitBounds() method to set the map to fit 
    // our markerBounds 
    map.fitBounds(markerBounds); 

    </script> 
</body> 
</html> 

Refrescante este ejemplo muchas veces, ningún marcador nunca se sale de la ventana gráfica:

fitBounds demo

+1

Gracias a todos ustedes, ¡eso fue exactamente lo que estaba buscando! – Calou

+0

gracias por comentar tu código. Muy muy útil. – MEM

6

Aquí es la forma:

map.fitBounds(bounds); 
map.setCenter(bounds.getCenter()); 

límites es una matriz de las coordenadas (marcadores). Cada vez que coloca marcador, haga algo como:

bounds.extend(currLatLng); 
+3

No creo que se necesite setCenter después de fitBounds – GilShalit

0

Calcular el zoom a la derecha para mostrar todos los marcadores no es trivial. Pero hay una función para eso: GMap.fitBounds() - define un límite (por ejemplo, los puntos más extremos de todas las coordenadas de marcador) y establece el mapa en consecuencia. Ver p. fitbounds() in Google maps api V3 does not fit bounds (la respuesta!) Para un buen ejemplo.

0

Ampliar centro depende de marcador único

var myCenter=new google.maps.LatLng(38.8106813,-89.9600172); 
var map; 
var marker; 
var mapProp; 
function initialize() 
{ 
    mapProp = { 
     center:myCenter, 
     zoom:8, 
     mapTypeId:google.maps.MapTypeId.ROADMAP 
    }; 

    map=new google.maps.Map(document.getElementById("map"),mapProp); 

    marker=new google.maps.Marker({ 
     position:myCenter, 
     animation:google.maps.Animation.BOUNCE, 
     title:"400 St. Louis Street Edwardsville, IL 62025" 
    }); 

    marker.setMap(map); 
    google.maps.event.addListener(map, "zoom_changed", function() { 
     map.setCenter(myCenter); 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 
Cuestiones relacionadas