kml - Openlayers obtaining centroid of a vector layer feature and using it to set the center of the map -
the code below uses absolute position center of map want set center according vector layer feature brought in kml file - possible? i've looked @ getcentroid can't syntax right?
map = new openlayers.map("map"); var mapnik = new openlayers.layer.osm(); //set centre position converting wgs mercator var wgs84 = new openlayers.projection("epsg:4326"); var fromprojection = new openlayers.projection("epsg:4326"); // transform wgs 1984 var toprojection = new openlayers.projection("epsg:900913"); // spherical mercator projection var position = new openlayers.lonlat(4.891280,52.373690).transform( fromprojection, toprojection); var layer = new openlayers.layer.vector("vectorlayer", { projection: wgs84, strategies: [new openlayers.strategy.fixed()], protocol: new openlayers.protocol.http({ url: "kmlfile.kml", //<-- relative or absolute url .osm file format: new openlayers.format.kml() }), stylemap: new openlayers.stylemap({ "default": { //pointradius: "${radius}", fillcolor: "blue", fillopacity: 0.1, strokecolor: "#5555ff", strokewidth: 2, strokeopacity: 0.8 } , "select": { fillcolor: "#8aeeef", strokecolor: "#32a8a9" } }) }); map.addlayers([mapnik, layer]); map.addcontrol(new openlayers.control.layerswitcher()); var scaleline = new openlayers.control.scaleline(); map.addcontrol(scaleline); //set centre of map , zoom level map.setcenter(position, 10 );
try (where layer vector based on code):
var bounds = layer.geometry.bounds; map.zoomtoextent(bounds);
or
var bounds = layer.geometry.bounds; map.setcenter(bounds.getcenterlonlat());
Comments
Post a Comment