Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm have been searching for several hours and do not find a way to get mouse coordinates after a SelectFeature, which returns the feature by itself. I saw that this has been asked several times earlier, so I try again !

Thanks very much,

Olivier

share|improve this question
    
I need an answer to this aswell. –  netbrain Mar 27 '12 at 9:12

2 Answers 2

You can get it with these ways:

feature.geometry.getBounds().getCenterLonLat()

or

map.events.register("click", map, function(e) { 
    var latlon = map.getLonLatFromViewPortPx(e.xy) ;
    alert(lonlat)
});

or

new OpenLayers.Control.SelectFeature(layer,{
    hover:true,
    eventListeners:{
      onSelect:function(e){
        alert(e.feature.geometry)
    }
});

or

layer.events.on({
            "featureselected": function(e) {       
            alert(e)
            }
});    

i hope it helps you....

share|improve this answer
    
Aragon, I think he is asking for mouse coordinates (mostly to display Popup) and not map coordinates. What your answer telling is to get map coordinates. –  Suraj Jul 1 at 10:49

my strategy (very resource intensive) is to listen to mousemove and constantly saving x and y values in global variables.

var mouseLat, mouseLon;
function init(){
  ...
  map.events.register("mousemove", map, function(e) {
    var position = this.events.getMousePosition(e);
    var lonlat = map.getLonLatFromPixel(position);
    var lonlatTransf = lonlat.transform(map.getProjectionObject(), proj4326);
    mouseLat = lonlatTransf.lat;
    mouseLon = lonlatTransf.lon;
    OpenLayers.Util.getElement("coords").innerHTML = "Lat: "+ mouseLat+" | Lon: "+mouseLon;
  });
  ...
}

when the feature is selected, i access these values from inside onFeatureSelect(event)

share|improve this answer
    
To decrease resource consumption, use debounce/throttle techniques benalman.com/projects/jquery-throttle-debounce-plugin –  ThomasG77 Nov 12 at 23:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.