Convert coordinates to pixels on screen (and back again)
The project and unproject methods make a lot of logic like this possible. If you have a click at a certain lngLat, and you want to position the map such that that geographic position is centered horizontally and at the 1/8 mark, you could:
- Take the geographic position and turn it into a pixel position with
project
- Shift that pixel position right by (1/2-1/8) = 4/8-1/8 = 3/8 =
0.375 * map._containerDimensions()[0]
, by adding that number to it - Turn that shifted pixel position back into a geographic position with
unproject
Or, you might also be able to do it the easy way by using the offset option in your flyTo
call.