3

好吧,当我测试这段代码时

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');

添加功能效果很好,但坐标为空。可能我错过了什么?是否也可以设置最大的可添加功能?

4

1 回答 1

6

lonlat 并不总是被定义。尝试:

        function getInfoFeature(event){
            var coords = event.geometry.getBounds().getCenterLonLat();
            console.log(coords);

        }
于 2012-06-13T10:54:21.983 回答