Credo che il titolo è sufficiente, non ho nemmeno vedere come fare questo per l'API V2 e V1:/Zoom e centro di una mappa di Google in base alle sue marcatori (API JavaScript V3)
Grazie :)
Credo che il titolo è sufficiente, non ho nemmeno vedere come fare questo per l'API V2 e V1:/Zoom e centro di una mappa di Google in base alle sue marcatori (API JavaScript V3)
Grazie :)
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:
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);
Non credo che setCenter sia necessario dopo fitBounds – GilShalit
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.
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);
Grazie a tutti voi, è stato esattamente quello che stavo cercando! – Calou
grazie per commentare il tuo codice. Molto molto utile. – MEM