views:

811

answers:

1

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  A: 

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.
Chris B
awesome, thanks!
Sander Versluys