Aditya Poddar
Aditya Poddar

Reputation: 1

Is there a method that converts screen coordinates into corresponding longitude and latitude values

I am using mapbox-gl js in my webpage. I want to display a marker on the map but I know it's position as it's distance from the top of the container and left side of the container.for example top:120px and left:80px. Is there any method which can convert this information i.e screen coordinates to mapbox's longitude and latitude coordinates. if there is no direct method please suggest a work around to convert screen coordinates to world coordinates.

Upvotes: 0

Views: 593

Answers (1)

taetscher
taetscher

Reputation: 76

from https://docs.mapbox.com/mapbox-gl-js/example/mouse-position/

this should do the trick:

    map.on('mousemove', function(e) {
        document.getElementById('info').innerHTML =
        // e.point is the x, y coordinates of the mousemove event relative
        // to the top-left corner of the map
        JSON.stringify(e.point) +
        '<br />' +
        // e.lngLat is the longitude, latitude geographical position of the event
        JSON.stringify(e.lngLat.wrap());

Upvotes: 1

Related Questions