I'm loading geojson from a Postgis-database and want to display it on my map.
After drawing a polygon, I want the map to zoom to the extents of the added polygon.
My data loads fine and displays correct on the map, but I cannot figure out how to get the bounds and change the zoom to the newly added polygon.
I tried to use parts of the code from Google's
Data Layer: Drag and Drop GeoJSON example,
but the displayed map zooms in somewhere in the Pacific Ocean close to the Baker Islands, while the polygon is displayed correctly in Luxembourg.
Here the code I am using:
window.addEventListener("load", func1);
function func1(){
//Load mapdata via geoJson
var parzelle = new google.maps.Data();
parzelle.loadGeoJson("./mapdata/get_parzelle_geojson.php<?php echo "?gid=".$_GET['gid'];?>");
// Set the stroke width, and fill color for each polygon
var featureStyle = {
fillColor: '#ADFF2F',
fillOpacity: 0.1,
strokeColor: '#ADFF2F',
strokeWeight: 1
}
parzelle.setStyle(featureStyle);
parzelle.setMap(map);
zoom(map);
}
function zoom(map) {
var bounds = new google.maps.LatLngBounds();
map.data.forEach(function(feature) {
processPoints(feature.getGeometry(), bounds.extend, bounds);
});
map.fitBounds(bounds);
}
function processPoints(geometry, callback, thisArg) {
if (geometry instanceof google.maps.LatLng) {
callback.call(thisArg, geometry);
} else if (geometry instanceof google.maps.Data.Point) {
callback.call(thisArg, geometry.get());
} else {
geometry.getArray().forEach(function(g) {
processPoints(g, callback, thisArg);
});
}
}
Is there a way to get that to work? It seems that there is no simple method to get the bounds of polygons in google.maps.data-layers
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…