Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a map location (lat, lng) pair to a screen position (x, y) pair in mapbox

I am using mapbox javascript API to do some cool stuff on a map. Now I have a map location (latitude, longitude) pair and I want to set the view to this location then use javascript to simulate a click at this location. The problem is that I need to find out the screen position (x, y) pair of this location after setting the view. But how do I do that?

It seems there is an API L.ICRS. I tried but it returned NaN for both x and y -- please check my example javascript codes below.

var latlng = [40.444621, -79.943231]
map.setView(latlng, 13);
L.CRS.EPSG3857.latLngToPoint(latlng, 13);

Can anybody help me on that? My final goal is to simulate a click at that position.

Thank you very much!

like image 450
Fei Avatar asked Apr 12 '14 05:04

Fei


1 Answers

Mapbox.js

Use the latLngToContainerPoint method off of the map object.

Mapbox GL JS

project is the equivalent function for GL.

like image 195
tmcw Avatar answered Sep 25 '22 17:09

tmcw