question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get coordinate on click

See original GitHub issue

How can we get the cursor coordinate using the onClick event on the map?

Here is how I’m trying:

_onClickMap(e) {
        console.log(e.lngLat);
    }

render() {
        return <div>
            <Map
                style="mapbox://styles/mapbox/outdoors-v10"
                containerStyle={{
                    height: 400,
                    width: "100vw",
                    marginTop: 60
                }}
                center={[-104.99, 39.75]}
                zoom={[9]}
                onClick={this._onClickMap}/>
           </div>

I would expect to be able to access the lngLat property similarly to this example: https://www.mapbox.com/mapbox-gl-js/example/mouse-position/

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

41reactions
alex3165commented, Jun 26, 2017

Almost that, sorry it is not clear in the documentation, here is what you need:

_onClickMap(map, evt) {
  console.log(evt.lngLat);
}
2reactions
LewisYoulcommented, Jan 11, 2018

Thanks so much for this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript get x and y coordinates on mouse click
Using event.clientX and **event.clientY provides the coordinates relative to the element taking into account margin, padding and border measures ...
Read more >
Getting the Mouse Click Position in JavaScript | kirupa.com
To get the position of all the elements between what you clicked on and the document, you have the getPosition function. This function...
Read more >
How to get the coordinates of a mouse click ... - GeeksforGeeks
The position of x-coordinate of the mouse click is found by subtracting the event's x position with the bounding rectangle's x position. The...
Read more >
How to Get Mouse Coordinates in JavaScript - Linux Hint
To get mouse coordinates in JavaScript, use clientX and clientY properties with onclick event, pageX and pageY properties or the onmousemove event.
Read more >
MouseEvent clientX Property - W3Schools
Output the coordinates of the mouse pointer when the mouse button is clicked on an element: var x = event.clientX; // Get the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found