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:

  1. Take the geographic position and turn it into a pixel position with project
  2. 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
  3. 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.