Get the maximum viewing distance from the current zoom level using the Google Maps API

When loading certain directions, the map adjusts to the appropriate zoom level.

Then I request several markers in the database at a certain radius from the center of the map.

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

Thanks!

+3
source share
1 answer

I think it’s easier and more accurate to get markers within the actual boundaries, not the radius. You can easily expand or reduce borders if you want.

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)";
}

, - :

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.
+3

Source: https://habr.com/ru/post/1717066/


All Articles