简体   繁体   中英

OpenLayers 2 - How to get coordinates for selected feature (point)

I'd like to get coordinates of selected point on my layer.

I found solution like this:

myLayer.features[0].geometry.getVertices()[0] 

but this is not certainly what I need, because it works only for one, concrete point.

I want to choose the point by clicking mouse and then get info about this.

Is it any solution for my problem?

this may help you:

new OpenLayers.Control.SelectFeature(layer,{
    hover:true,
    eventListeners:{
        onSelect:function(e){
            alert(e.feature.geometry.getVertices()[0].x);
            alert(e.feature.geometry.getVertices()[0].y);
        }
    }    
});

Ok, I found solution for me: (I'm using GeoExt). I've just added to my code it:

 new OpenLayers.Layer.Vector("warstwa", {
     styleMap: new OpenLayers.StyleMap({
         'default': styl
     }),
     protocol: new OpenLayers.Protocol.HTTP({
         url: '',
         format: new OpenLayers.Format.GeoJSON()
     }),
     strategies: [new OpenLayers.Strategy.Fixed()],      
         eventListeners: {
             featureselected: function(e) {
             var xValue = Ext.getCmp('xValue');
             var yValue = Ext.getCmp('yValue');
             xValue.setValue(e.feature.geometry.getVertices()[0].x);
             yValue.setValue(e.feature.geometry.getVertices()[0].y);
        }
    });

...and it works for me :)

You can found the samples of the OpenLayers Cookbook at http://acanimal.github.io/Openlayers-Cookbook/ (source https://github.com/acanimal/Openlayers-Cookbook ) which contains samples for feature selection.

Cheers.

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