[英]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). 好的,我找到了适合我的解决方案:(我正在使用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. 您可以在http://acanimal.github.io/Openlayers-Cookbook/ (来源https://github.com/acanimal/Openlayers-Cookbook )找到OpenLayers Cookbook的样本,其中包含用于功能选择的样本。
Cheers. 干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.