3
votes

When loading some directions the map adjusts to the appropriate zoom level.

Then I request for several markers in the database within a certain radius from the map's center.

How do i get the correct radius value depending on the current zoom level? This value should be the real distance from the center to the edge of the map.

Thanks!

1
Top/bottom or left/right edge? - Crescent Fresh
well, considering i need the distance from the center of the map, i doesn't really matter... but let's say, top/left edge. - Sander Versluys
Do you want to get all the markers with a radius from the center of the map, or all the markers that are within the bounds of the map? Those are two different things. - Chris B
well, the markers within the bounds would be best but i've already managed that with sql within a radius, i just need to know the max distance of the map (which depends on the zoom level) to feed my query - Sander Versluys

1 Answers

4
votes

I think it's easier and more accurate to get the markers within the actual bounds, instead of a radius. You could easily expand or shrink the bounds if you wish.

Javascript:

var bounds = map.getBounds();
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();

var s = sw.lat();
var w = sw.lng();
var n = ne.lat();
var e = ne.lng();

PHP/SQL:

$query = "SELECT * FROM Markers WHERE ";
if ($w > $e) { // If the bounds include the intl. date line
    $query .= "(lat BETWEEN $s AND $n) AND ((lng BETWEEN -180 AND $e) OR (lng BETWEEN $w AND 180))";
} else {
    $query .= "(lat BETWEEN $s AND $n) AND (lng BETWEEN $w AND $e)";
}

But if you really want to go with the radius method, you can do something like this:

var w = map.getBounds().getSouthWest().lng();
var centerLng = map.getCenter().lng();

var distance = centerLng - w; // This is the distance from the center to the left edge in degrees.