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";
for future reference:
getMapRadiusKM: function() {
var mapBoundNorthEast = map.getBounds().getNorthEast();
var mapDistance = mapBoundNorthEast.distanceTo(map.getCenter());
return mapDistance/1000;
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With