You can use the containerPointToLatLng L.Map transform function to get latLng coordinates for a given pixel coordinator. If you take one of the first pixel and one of the following, you can use the distanceTo utility for L.LatLng to calculate the distance in meters between them. See the following code (assuming the map is an instance of L.Map):
var centerLatLng = map.getCenter(); // get map center var pointC = map.latLngToContainerPoint(centerLatLng); // convert to containerpoint (pixels) var pointX = [pointC.x + 1, pointC.y]; // add one pixel to x var pointY = [pointC.x, pointC.y + 1]; // add one pixel to y // convert containerpoints to latlng's var latLngC = map.containerPointToLatLng(pointC); var latLngX = map.containerPointToLatLng(pointX); var latLngY = map.containerPointToLatLng(pointY); var distanceX = latLngC.distanceTo(latLngX); // calculate distance between c and x (latitude) var distanceY = latLngC.distanceTo(latLngY); // calculate distance between c and y (longitude)
This should work, thanks to Jarek Piorkowski for pointing out my mistake before editing.
iH8
source share