OpenLayers SelectFeature and XY

I need to know the exact point the function was clicked on. Using SelectFeature I can get information about which function was pressed, but there is no location information on the map.

Here is the code that the listner function creates:

select = new OpenLayers.Control.SelectFeature(
            [vectorLayer],
            {
                clickout: false, toggle: false,
                multiple: false, hover: false
            }
        );

osMap.addControl(select); 

and here is my definition of a listener:

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 = osMap.getLonLatFromViewPortPx(e.xy);

            }
});    

thank

+3
source share
3 answers

Niklas is right, I use something similar for popups:

var popup = new OpenLayers.Popup.Anchored(
  "popup", 
  map.getLonLatFromPixel(evt.xy),
  null,
  evt.text,
  null,
  false
);

You can use the MousePosition control if it is evtnot available:

 map.getLonLatFromPixel(
   (map.getControlsByClass("OpenLayers.Control.MousePosition‌​")[0]).lastXy
 ) 
+5
source

Look at the getLonLatFromPixel function on the Map object along with the e.xy property.

EDIT: , . , xy .

+2

take a look at http://trac.osgeo.org/openlayers/ticket/2089 , the patch is submitted 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]);
    }
});
+1
source

Source: https://habr.com/ru/post/1795212/


All Articles