2010-08-19 13 views

risposta

70

Come suggerito dalle altre risposte, il metodo fitBounds() dovrebbe fare il trucco.

consideri il seguente esempio, che genererà 10 punti casuali sugli Stati Uniti nord-est, e si applica il metodo fitBounds():

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

rinfrescante questo esempio molte volte, nessun marcatore va mai al di fuori della finestra:

fitBounds demo

+1

Grazie a tutti voi, è stato esattamente quello che stavo cercando! – Calou

+0

grazie per commentare il tuo codice. Molto molto utile. – MEM

6

Qui è il modo:

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

limiti è un array di coordinate (marcatori). Ogni volta che si posiziona un marcatore fai qualcosa del tipo:

bounds.extend(currLatLng); 
+3

Non credo che setCenter sia necessario dopo fitBounds – GilShalit

0

Calcolare lo zoom destro per mostrare tutti i tuoi marcatori non è banale. Ma c'è una funzione per questo: GMap.fitBounds() - tu definisci un limite (ad esempio, i punti più estremi di tutte le coordinate del tuo marcatore) e imposta la mappa di conseguenza. Vedi per es. fitbounds() in Google maps api V3 does not fit bounds (la risposta!) Per un buon esempio.

0

Centro Zoom dipende singolo marcatore

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); 
Problemi correlati