Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

leaflet.js calculating radius from center to borders

I'm switching from using google maps to leaflet.js. One thing I did in google maps and can't seem to find in leaflet.js is calculate the radius from the center of the map (i.e. search location) to the sides of the map. As you can zoom in and out the area that people are looking at can change significantly. The code below showed the few lines I had in order to do that with google maps. Can somebody point me in the right direction regarding leaflet.js?

  // viewport stores the recommended viewport for the returned result.
  // (LatLngBounds)
  viewportLatLngBounds = zip.get("location").geometry.viewport;

  this.map.fitBounds(viewportLatLngBounds);

  this.collection.latitude = viewportLatLngBounds.getCenter().lat();
  this.collection.longitude = viewportLatLngBounds.getCenter().lng();

  // calculate radius
  // get distance..
  // from (lat of NE corner), (lng of center)
  // to   (lat of center), (lng of center)
  topCenterLatLng = new google.maps.LatLng(viewportLatLngBounds.getNorthEast().lat(), viewportLatLngBounds.getCenter().lng());

  metersRadius = google.maps.geometry.spherical.computeDistanceBetween(viewportLatLngBounds.getCenter(), topCenterLatLng);

  this.collection.radius = metersRadius / 1000;
  this.collection.radiusUnits = "km";
like image 648
Rudi Avatar asked Dec 04 '22 13:12

Rudi


1 Answers

for future reference:

getMapRadiusKM: function() {
    var mapBoundNorthEast = map.getBounds().getNorthEast();
    var mapDistance = mapBoundNorthEast.distanceTo(map.getCenter());
    return mapDistance/1000;
},
like image 67
Rudi Avatar answered Mar 12 '23 15:03

Rudi