2009-10-12 18 views
274

Sono passato di recente all'API di Google Maps API V3. Sto lavorando ad un semplice esempio che traccia marcatori da un array, tuttavia non so come centrare e ingrandire automaticamente rispetto ai marker.API Google Maps v3 - imposta limiti e centro

Ho cercato la rete alta e bassa, inclusa la documentazione di Google, ma non ho trovato una risposta chiara. So che potrei semplicemente prendere una media delle coordinate, ma come impostare lo zoom di conseguenza?

function initialize() { 
    var myOptions = { 
    zoom: 10, 
    center: new google.maps.LatLng(-33.9, 151.2), 


    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions); 

    setMarkers(map, beaches); 
} 


var beaches = [ 
    ['Bondi Beach', -33.890542, 151.274856, 4], 
    ['Coogee Beach', -33.423036, 151.259052, 5], 
    ['Cronulla Beach', -34.028249, 121.157507, 3], 
    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], 
    ['Maroubra Beach', -33.450198, 151.259302, 1] 
]; 

function setMarkers(map, locations) { 

    var image = new google.maps.MarkerImage('images/beachflag.png', 
     new google.maps.Size(20, 32), 
     new google.maps.Point(0,0), 
     new google.maps.Point(0, 32)); 
    var shadow = new google.maps.MarkerImage('images/beachflag_shadow.png', 

     new google.maps.Size(37, 32), 
     new google.maps.Point(0,0), 
     new google.maps.Point(0, 32)); 


     var lat = map.getCenter().lat(); 
     var lng = map.getCenter().lng();  


    var shape = { 
     coord: [1, 1, 1, 20, 18, 20, 18 , 1], 
     type: 'poly' 
    }; 
    for (var i = 0; i < locations.length; i++) { 
    var beach = locations[i]; 
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]); 
    var marker = new google.maps.Marker({ 
     position: myLatLng, 
     map: map, 
     shadow: shadow, 
     icon: image, 
     shape: shape, 
     title: beach[0], 
     zIndex: beach[3] 
    }); 
    } 
} 

risposta

164

Hai tutto ordinato - vedere le ultime righe di codice - (bounds.extend(myLatLng); map.fitBounds(bounds);)

function initialize() { 
    var myOptions = { 
    zoom: 10, 
    center: new google.maps.LatLng(0, 0), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    var map = new google.maps.Map(
    document.getElementById("map_canvas"), 
    myOptions); 
    setMarkers(map, beaches); 
} 

var beaches = [ 
    ['Bondi Beach', -33.890542, 151.274856, 4], 
    ['Coogee Beach', -33.923036, 161.259052, 5], 
    ['Cronulla Beach', -36.028249, 153.157507, 3], 
    ['Manly Beach', -31.80010128657071, 151.38747820854187, 2], 
    ['Maroubra Beach', -33.950198, 151.159302, 1] 
]; 

function setMarkers(map, locations) { 
    var image = new google.maps.MarkerImage('images/beachflag.png', 
    new google.maps.Size(20, 32), 
    new google.maps.Point(0,0), 
    new google.maps.Point(0, 32)); 
    var shadow = new google.maps.MarkerImage('images/beachflag_shadow.png', 
    new google.maps.Size(37, 32), 
    new google.maps.Point(0,0), 
    new google.maps.Point(0, 32)); 
    var shape = { 
    coord: [1, 1, 1, 20, 18, 20, 18 , 1], 
    type: 'poly' 
    }; 
    var bounds = new google.maps.LatLngBounds(); 
    for (var i = 0; i < locations.length; i++) { 
    var beach = locations[i]; 
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]); 
    var marker = new google.maps.Marker({ 
     position: myLatLng, 
     map: map, 
     shadow: shadow, 
     icon: image, 
     shape: shape, 
     title: beach[0], 
     zIndex: beach[3] 
    }); 
    bounds.extend(myLatLng); 
    } 
    map.fitBounds(bounds); 
} 
+6

Grazie! I documenti 3.0 sono sorprendentemente vaghi su dove questa funzionalità è andata. – Bill

+8

I documenti 3.0 sono sorprendentemente vaghi su dove sono passate molte cose. :( – Scottie

+0

assolutamente perfetto, grazie – Rabbott

382

Sì, è sufficiente dichiarare il tuo nuovo oggetto limiti.

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

Poi, per ogni indicatore, estendere l'oggetto limiti:

bounds.extend(myLatLng); 
map.fitBounds(bounds); 

API: google.maps.LatLngBounds

+34

potresti anche aggiungere questo map.setCenter (bounds.getCenter()); –

+0

Sia la risposta che il commento di Raman hanno contribuito a ridimensionare la mia attenzione su ciò di cui avevo bisogno. – JustJohn

+0

@ Sam152: potresti essere interessato a [500 combinazioni di bounty per una domanda correlata] (http://stackoverflow.com/questions/27094908/google-maps-keep-center-when-zooming). –

-15

di utilizzare una,

map.setCenter (bounds.getCenter(), mappa .getBoundsZoomLevel (limiti));

+12

Questo è per Google Maps API v2 – dave1010

+0

È necessario fornire una soluzione più ponderata. E credo che questo sia per Google Maps v2. – AllJs

-1

Il metodo setCenter() è ancora applicabile all'ultima versione dell'API di Maps per Flash in cui fitBounds() non esiste.

5

Il mio suggerimento per google maps api v3 sarebbe (non credo che possa essere fatto più efficientemente):

gmap : { 
    fitBounds: function(bounds, mapId) 
    { 
     //incoming: bounds - bounds object/array; mapid - map id if it was initialized in global variable before "var maps = [];" 
     if (bounds==null) return false; 
     maps[mapId].fitBounds(bounds); 
    } 
} 

Nel risultato u adattarsi a tutti i punti di limiti nella vostra finestra della mappa.

esempio funziona perfettamente e u liberamente può controllare qui www.zemelapis.lt

+0

Invece di restituire 'false' se i' bounds 'sono 'null', potresti' maps [mapId] .getBounds() '. – kaiser

+0

@localtime in realtà, il tuo sito web ha bisogno delle chiavi API di Google Maps per funzionare –

0

Le risposte sono perfetti per regolare mappa dei confini per i marcatori, ma se vi piace di espandere Google Maps confini per le forme come poligoni e cerchi, è possibile utilizzare seguenti codici:

per i cerchi

bounds.union(circle.getBounds()); 

per i poligoni

polygon.getPaths().forEach(function(path, index) 
{ 
    var points = path.getArray(); 
    for(var p in points) bounds.extend(points[p]); 
}); 

per i rettangoli

bounds.union(overlay.getBounds()); 

per le polilinee

var path = polyline.getPath(); 

var slat, blat = path.getAt(0).lat(); 
var slng, blng = path.getAt(0).lng(); 

for(var i = 1; i < path.getLength(); i++) 
{ 
    var e = path.getAt(i); 
    slat = ((slat < e.lat()) ? slat : e.lat()); 
    blat = ((blat > e.lat()) ? blat : e.lat()); 
    slng = ((slng < e.lng()) ? slng : e.lng()); 
    blng = ((blng > e.lng()) ? blng : e.lng()); 
} 

bounds.extend(new google.maps.LatLng(slat, slng)); 
bounds.extend(new google.maps.LatLng(blat, blng)); 
Problemi correlati