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.

7 Answers 7


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
                    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);

function getCoordinates(e) {
 // this should work
 var lonlat = map.getLonLatFromViewPortPx(e.xy);
 alert("You clicked near " + lonlat.lat + " N, " +
                                          + lonlat.lon + " E");
  • 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, 2011 at 21:02
  • @Vish : in that case, see Tim Schaub's answer and my edit above.
    – simo
    Nov 16, 2011 at 7:57
  • Hey simo, where is the 'e' in the getCoordinates method coming from?
    – Vish
    Nov 17, 2011 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, 2011 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, 2012 at 20:15
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.

  • This is the simplest and easiest way. Can confirm it also works with v5.3.
    – Raidok
    Nov 21, 2018 at 22:39
  • 1
    This is the best answer now. Better still is to replace the hardcode source projection with evt.map.getView().getProjection(). Apr 8, 2020 at 3:41

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.


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

Inside the clickFeature handler

var clickedlonlat = 
        new OpenLayers.Pixel(

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

  • This worked, but I had to remove Ext.getCmp("coreRef").parent from the code Apr 2, 2018 at 3:39

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.

  • 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, 2011 at 21:00

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.


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.

  • 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, 2020 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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