Leaflet convert meters to pixels

762 Views Asked by At

I am creating an app using leaflet

I have the leaflet map up and running and have a Geoserver service that return points that are shown on the map.

I have one additional field that is distance between my Points on the Map and it is in meters ?

My question is how could I convert it to pixels? Is there any Leaflet function or anything ?

1

There are 1 best solutions below

3
On BEST ANSWER

You can use this function with the L.GeometryUtil library.

L.GeometryUtil CDN

function disToPixeldistance(distance){
  var l2 = L.GeometryUtil.destination(map.getCenter(),90,distance);
  var p1 = map.latLngToContainerPoint(map.getCenter())
  var p2 = map.latLngToContainerPoint(l2)
  return p1.distanceTo(p2)
}

But think of that the pixel distance is changing every time you zoom