OpenLayers SelectFeature and XY
I need to know exact point that was clicked on a feature. Using SelectFeature I can get info about which feature was clicked but there is no info about a location on the map.
Here is the code that creates a listner function:
select = new OpenLayers.Control.SelectFeature(
[vectorLayer],
{
clickout: false, toggle: false,
multiple: false, hover: false
}
);
osMap.addControl(select);
and here is my listener's definition:
vectorLayer.events.on({
"featureselected": function(e) {
//here I need to get XY
//something like the code below
//(it doesn't work but clearly explains what my idea is)
var lonlat =开发者_StackOverflow社区 osMap.getLonLatFromViewPortPx(e.xy);
}
});
Thanks
Niklas is right, I use something like that for popups :
var popup = new OpenLayers.Popup.Anchored(
"popup",
map.getLonLatFromPixel(evt.xy),
null,
evt.text,
null,
false
);
You may use the MousePosition control if evt
isn't available:
map.getLonLatFromPixel(
(map.getControlsByClass("OpenLayers.Control.MousePosition")[0]).lastXy
)
Look at the getLonLatFromPixel function on the Map object, together with the e.xy property.
EDIT: Also, check this setting on events. It looks as you could extract xy property from all types of mouse events.
look at http://trac.osgeo.org/openlayers/ticket/2089, a patch is presented using this.handlers.feature.evt
new OpenLayers.Control.SelectFeature([layer],{
hover:true,
eventListeners:{
featurehighlighted:function(e){
console.log(this.handlers.feature.evt.clientX-map.div.offsets[0]);
console.log(this.handlers.feature.evt.clientY-map.div.offsets[1]);
}
});
精彩评论