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 need to get the coordinate of the click when the user clicks on a vector feature on the OpenLayers map. The SelectControl only gives the feature that was clicked and not the coordinates of the click. Anyway to get at the coordinates of the click on a vector? I need to show AnchoredBubble at the point of the user click.

share|improve this question

4 Answers 4

You can use feature.getBounds().getCenterLonLat(). Works for point/line/polygon type features. And you don't have to know what it is, since it works for all.

share|improve this answer
    
Hi Vadim, I need the coordinate of the click since i want to anchor my bubble there and not at the center. Thank You, –  Vish Nov 15 '11 at 21:00

Actually, the click event sample gives you what you want.

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {                
            defaultHandlerOptions: {
                'single': true,
                'double': false,
                'pixelTolerance': 0,
                'stopSingle': false,
                'stopDouble': false
            },

            initialize: function(options) {
                this.handlerOptions = OpenLayers.Util.extend(
                    {}, this.defaultHandlerOptions
                );
                OpenLayers.Control.prototype.initialize.apply(
                    this, arguments
                ); 
                this.handler = new OpenLayers.Handler.Click(
                    this, {
                        'click': this.trigger
                    }, this.handlerOptions
                );
            }, 

            trigger: function(e) {
                var lonlat = map.getLonLatFromViewPortPx(e.xy);
                alert("You clicked near " + lonlat.lat + " N, " +
                                          + lonlat.lon + " E");
            }

        });

If needed you can convert coordinates to pixel to display the popup.

Edit - to get coordinates only on selecting feature :

   var options = {
    onSelect: getCoordinates,
};

var selectEt = new OpenLayers.Control.SelectFeature(mylayer, options);
map.addControl(selectEt);

function getCoordinates(e) {
 // this should work
 var lonlat = map.getLonLatFromViewPortPx(e.xy);
 alert("You clicked near " + lonlat.lat + " N, " +
                                          + lonlat.lon + " E");
}
share|improve this answer
    
Hi simo, I need the click event to be called only when clicking on the vector layer or feature I am interested in. I think the sample above captures and calls click event handler on the entire map and not just the vector layers. Thank You, Vish –  Vish Nov 15 '11 at 21:02
    
@Vish : in that case, see Tim Schaub's answer and my edit above. –  simo Nov 16 '11 at 7:57
    
Hey simo, where is the 'e' in the getCoordinates method coming from? –  Vish Nov 17 '11 at 2:39
    
e is the event. I added it to the declaration of the function getCoordinates(e). I'm sure you can pass the feature, but I'm not sure about the event. Make a test. –  simo Nov 17 '11 at 9:50
    
@simo, it says that onSelect should expect to be called with a feature, not an event: dev.openlayers.org/docs/files/OpenLayers/Control/… –  Chris Aug 2 '12 at 20:15

The API doesn't provide a way to get the click location from the SelectFeature control - but it should. It would be a trivial addition (to have xy included in the featureselected event). If you ticket this, it would be the first step in making it happen.

In the meantime, you can access the mouse event on the feature handler used by the SelectFeature control. So, you might have a listener that looks something like this:

layer.events.on({featureselected: function(event) {
    // should be event.xy, but it's not available currently
    var pixel = control.handlers.feature.evt.xy;
    var location = map.getLonLatFromPixel(pixel);
    alert("You clicked near " + location);
});

This assumes (obviously) that you have a reference to the SelectFeature control and your map.

share|improve this answer

I was able to get the lat lon of the click event using the following:

Inside the clickFeature handler

var clickedlonlat = 
    Ext.getCmp("coreRef").parent.map.getLonLatFromPixel(
        new OpenLayers.Pixel(
            selectFeatureReference.handlers.feature.evt.layerX,
            selectFeatureReference.handlers.feature.evt.layerY
        ));

where selectFeatureReference is the reference to the SelectFeature that you created.

share|improve this answer

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.