0
下面的代碼使用地圖中心的絕對位置,但我想根據從中引入的矢量圖層要素設置中心KML文件 - 這可能嗎?我已經看過getCentroid,但無法獲得正確的語法?Openlayers獲取矢量圖層要素的質心並使用它來設置地圖的中心
map = new OpenLayers.Map("Map");
var mapnik = new OpenLayers.Layer.OSM();
//Set centre position converting from WGS to Mercator
var wgs84 = new OpenLayers.Projection("EPSG:4326");
var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
var toProjection = new OpenLayers.Projection("EPSG:900913"); // to 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 to your .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 and zoom level
map.setCenter(position, 10);
格里,這並不工作 - 地圖加載,但我只得到白色畫面對於地圖和比例尺在10000km - 如果我放大一次,那麼世界地圖會出現,而不是以KML功能爲中心。 –
,如果你用'layer'替換'bounds'? – Gery