L'API di Google Maps v3 non supporta in modo nativo un metodo getBounds()
per la classe google.maps.Polygon. Questo sembra strano dato che la classe google.maps.Map ha un metodo fitBounds()
, che è esattamente quello che stai cercando ... Se usi l'API di Google Maps con qualsiasi tipo di frequenza, questo dovrebbe essere nella tua borsa di trucchi ::
getBounds()
metodo per la classe google.maps.Polygon
google.maps.Polygon.prototype.getBounds = function() {
var bounds = new google.maps.LatLngBounds();
var paths = this.getPaths();
var path;
for (var i = 0; i < paths.getLength(); i++) {
path = paths.getAt(i);
for (var ii = 0; ii < path.getLength(); ii++) {
bounds.extend(path.getAt(ii));
}
}
return bounds;
}
Avere questo metodo a portata di mano lo rende molto semplice da centrare e montare un poligono sulla mappa.
Nota: se non si ha familiarità con getAt()
e getLength()
, va bene, sono metodi unici per la classe google.maps.MVCArray. Quando chiami il metodo getPaths()
di un poligono restituisce la tua matrice di LatLng come un MVCArray mutabile di LatLng's .. puoi essere pronto su MVCArrays qui - Google Maps v3 API MVCArray class.
In movimento. Ecco il framework, dovrai implementare il metodo prototipato sopra da qualche parte prima di questo prossimo bit di codice.
var map = new google.maps.Map(container, opts); // I'll spare the details on this
var coords = [
new google.maps.LatLng(25.774252, -80.190262)
,new google.maps.LatLng(18.466465, -66.118292)
,new google.maps.LatLng(32.321384, -64.75737)
,new google.maps.LatLng(25.774252, -80.190262)
];
var myPolygon = new google.maps.Polygon({
paths: coords
,strokeColor: "#A80000"
,strokeOpacity: 0.8
,strokeWeight: 1
,fillColor: "#0b2a32"
,fillOpacity: 0.12
});
Con la scenografia, tutto ciò che avrebbe dovuto fare per centrare e zoomare su questo (o qualsiasi) poligono è ::
map.fitBounds(myPolygon.getBounds());
breve e dolce. Spero possa aiutare.
-Kevin James
Questo non è più vero! Maps v3 ha scaricato il metodo getBounds(). – Benjamin
controlla questo link per estendere il poligono per ottenere limiti (Google Map V3), http://tutorialspots.com/google-maps-javascript-api-v3-method-polygon-getbounds-515.html – shyammtp