Ho questo codice che rende una mappa.
function initialize() { var myOptions = { center: new google.maps.LatLng(45.4555729, 9.169236), zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP, panControl: true, mapTypeControl: false, panControlOptions: { position: google.maps.ControlPosition.RIGHT_CENTER }, zoomControl: true, zoomControlOptions: { style: google.maps.ZoomControlStyle.LARGE, position: google.maps.ControlPosition.RIGHT_CENTER }, scaleControl: false, streetViewControl: false, streetViewControlOptions: { position: google.maps.ControlPosition.RIGHT_CENTER } }; var map = new google.maps.Map(document.getElementById("mapCanvas"), myOptions); var Item_1 = new google.maps.LatLng(45.5983128 ,8.9172776); var myPlace = new google.maps.LatLng(45.4555729, 9.169236); var marker = new google.maps.Marker({ position: Item_1, map: map}); var marker = new google.maps.Marker({ position: myPlace, map: map}); var bounds = new google.maps.LatLngBounds(myPlace, Item_1); map.fitBounds(bounds); }
Anche se i due punti sono separati da 25 km ottengo questo risultato:
Mentre mi piacerebbe rendere uno zoom di livello superiore.
Come questo
Io uso il metodo fitBounds ()
var bounds = new google.maps.LatLngBounds(myPlace, Item_1); map.fitBounds(bounds);
Grazie per il vostro sostegno
Ciò accade perché LatLngBounds()
non prende due punti arbitrari come parametri, ma i punti SW e NE
usa il metodo .extend()
su un object bounds vuoto
var bounds = new google.maps.LatLngBounds(); bounds.extend(myPlace); bounds.extend(Item_1); map.fitBounds(bounds);
Demo su http://jsfiddle.net/gaby/22qte/
LatLngBounds
deve essere definito con i punti in ordine (sud-ovest, nord-est). I tuoi punti non sono in questo ordine.
La correzione generale, soprattutto se non si conoscono i punti sarà sicuramente in questo ordine, è di estendere un limite vuoto:
var bounds = new google.maps.LatLngBounds(); bounds.extend(myPlace); bounds.extend(Item_1); map.fitBounds(bounds);
L’API risolverà i limiti.
Ho lo stesso problema che descrivi anche se sto costruendo il mio LatLngBounds come proposto sopra. Il problema è che le cose sono asincrone e chiamare map.fitBounds()
nel momento sbagliato potrebbe lasciare un risultato come nella Q. Il modo migliore che ho trovato è di mettere la chiamata in un gestore inattivo come questo:
google.maps.event.addListenerOnce(map, 'idle', function() { map.fitBounds(markerBounds); });
var map = new google.maps.Map(document.getElementById("map"),{ mapTypeId: google.maps.MapTypeId.ROADMAP }); var bounds = new google.maps.LatLngBounds(); for (i = 0; i < locations.length; i++){ marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map }); bounds.extend(marker.position); } map.fitBounds(bounds);