好吧,当我测试这段代码时
var map;
function mapInit(element){
map = new OpenLayers.Map(element);
var osm = new OpenLayers.Layer.OSM();
var vectors = new OpenLayers.Layer.Vector("Vector Layer", {renderers: renderer});
var renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;
map.addLayers([osm, vectors]);
map.zoomToMaxExtent();
var add = new OpenLayers.Control.DrawFeature(vectors,OpenLayers.Handler.Point, {
featureAdded: getInfoFeature,
multi: false
});
map.addControl(add);
add.activate();
}
function getInfoFeature(event){
var coords = event.lonlat;
console.log(coords);
}
mapInit('siteMap');
添加功能很有用,但坐标为空。可能我错过了什么?是否也可以设置最多可添加的功能?
答案 0 :(得分:6)
并不总是定义lonlat。 试试:
function getInfoFeature(event){
var coords = event.geometry.getBounds().getCenterLonLat();
console.log(coords);
}