-
Hi! How can I get the view radius of the map component according to the zoom level? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You can get the bounds of the map instance using function getBoundsRadius(bounds) {
// r = radius of the earth in km
const r = 6378.8;
// degrees to radians (divide by 57.2958)
const neLat = bounds.getNorthEast().lat() / 57.2958;
const neLng = bounds.getNorthEast().lng() / 57.2958;
const cLat = bounds.getCenter().lat() / 57.2958;
const cLng = bounds.getCenter().lng() / 57.2958;
// distance = circle radius from center to Northeast corner of bounds
const radius =
r *
Math.acos(
Math.sin(cLat) * Math.sin(neLat) +
Math.cos(cLat) * Math.cos(neLat) * Math.cos(neLng - cLng),
);
return radius; // radius in meters
} |
Beta Was this translation helpful? Give feedback.
-
I found today, the hard way, that the radius only really works if your viewport is a perfect circle. You likely want a radius of your viewable area only. If this is the case, you can utilize the google calculation
|
Beta Was this translation helpful? Give feedback.
You can get the bounds of the map instance using
map.getBounds()
, and then use this function/formula: