24

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.

OhadR
  • 115
  • 4
Vish
  • 607
  • 2
  • 6
  • 11

7 Answers7

17

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");
}
Shiko
  • 2,883
  • 13
  • 22
simo
  • 8,580
  • 1
  • 28
  • 56
  • 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 07:57
  • Hey simo,

    where is the 'e' in the getCoordinates method coming from?

    – Vish Nov 17 '11 at 02: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 09:50
  • @simo, it says that onSelect should expect to be called with a feature, not an event: http://dev.openlayers.org/docs/files/OpenLayers/Control/SelectFeature-js.html#OpenLayers.Control.SelectFeature.onSelect – Chris Aug 02 '12 at 20:15
  • I have almost the same question with only difference that the click happens when drawing starts or finishes. I want to extract the coordinates of these two points. Could you please take a look at my question too: http://gis.stackexchange.com/questions/168331/how-to-get-the-coordinates-of-the-starting-and-ending-points-of-a-line-in-ol-int – msc87 Oct 30 '15 at 09:36
11
map.on('click', function(evt){
    console.log(ol.proj.transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326'));
});

This is how I figured it out in v3.8.2 in order to get coordinates like the following:
[-1.1645507812500016, 53.2257684357902]
when I click around UK.

khandaniel
  • 211
  • 2
  • 5
9

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.

Tim Schaub
  • 836
  • 5
  • 4
3

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.

Chris
  • 474
  • 3
  • 10
0

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.

Vadim
  • 3,971
  • 2
  • 29
  • 42
  • 1
    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
0

In case anyone stumbles across this old question like I did, in the latest release of OpenLayers at this time (3.20.0) you can get the clicked position using e.mapBrowserEvent.coordinate where e is the Select event.

Adam Franco
  • 243
  • 1
  • 8
0

I wanted to be able to copy the coordinates with no extra characters using OL6, but lat and lng are available as needed.

  import { transform } from 'ol/proj';
  map.on('click', function(evt){
      var lonlat = transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326');
      var lon = lonlat[0];
      var lat = lonlat[1];
      alert("You clicked near lat lon: "+ lon.toFixed(6) + "  " + lat.toFixed(6));
    });

This is not original to me, I just put the right pieces together.

Greg
  • 899
  • 1
  • 7
  • 16
  • my click event does not hae "coordinate", i had to use values evt.map.pixelToCoordinateTransform_[4] and evt.map.pixelToCoordinateTransform_[5]. i really don't understand this library – user151496 Aug 19 '20 at 09:55