简体   繁体   中英

Get LonLat from added feature in OpenLayers

Well, when I am testing this code

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

It is working good to add features, but the coordinates is null. And probably I've missed something? Is that also possible to set a maximum of addable features?

lonlat is not always defined. Try with :

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

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM