There are various ways to achieve your goal. If you have access to the longitude and latitude of users (which seems to be the case), instead of retrieving all user locations with
(select latitude, longitude from users)
, you can narrow down the search area by setting a specific range for the user's coordinates in your query. Here's an example of how your select statement could look like:
select latitude, longitude from users where latitude between (user.latitude + desiredRange and user.latitude - desiredRange) AND longitude between (user.longitude + desiredRange AND user.longitude - desiredRange);
You can determine the appropriate range based on the map.bounds property.
After obtaining the relevant data, you can update your script using a service that will remove existing markers on the map and add new ones based on the response received. This is an approximation of how your JavaScript code might appear:
get user location;
send service request for user locations and receive them in an array
remove current markers
add new markers based on service response
The PHP service implementation could resemble the following:
$minLat = $_REQUEST['lat'] - ($_REQUEST['dl'] / 2);
$maxLat = $_REQUEST['lat'] + ($_REQUEST['dl'] / 2);
$minLon = $_REQUEST['lon'] - $_REQUEST['dln'];
$maxLon = $_REQUEST['lon'] + $_REQUEST['dln'];
$query = 'SELECT locations.latitude,
locations.longitude
FROM locations
WHERE (latitude BETWEEN ' . $minLat . ' AND ' . $maxLat . ') AND
(longitude BETWEEN ' . $minLon . ' AND ' . $maxLon. ')';
}
$query = DB::query($query);
$json = array();
while($location = $query->fetch_assoc()) {
$json[] = $location;
}
header('Content-Type: application/json');
echo json_encode($json);
exit();
I hope this information proves useful to you.