目前,我已经创建了一个页面,显示了谷歌地图上所有用户的位置(全部来自MySQL数据库)。但是因为有5000+用户位置,所以加载页面需要很长时间。
我想通过只查询特定区域内的用户位置来使其更有效率。(我正在查看的区域)
我能做些什么来让它更有效率?google maps API是否支持我想要的功能?我听说我可以做地理栅栏mysql,但我真的不知道如何使用它。
提前谢谢你。

发布于 2017-08-01 11:17:05
可能有很多方法可以做你想做的事情。如果您知道用户的经度和纬度(听起来像是这样),那么不用加载所有用户位置(select latitude, longitude from users),然后通过指定用户坐标的范围来缩小位置范围,这样您的select就像这样(在伪查询中)
select latitude, longitude from users where latitude between (user.latitude + whateverrangeyouwant and user.latitude - whateverrangeyouwant) AND longitude between (user.longitude + whateverrangeyouwant AND user.longitude - whateverrangeyouwant);该范围可能取自map.bounds属性
然后,您可以使用服务将其发送回您的脚本,该服务随后将从地图中删除标记,并根据您的服务响应添加一个标记。所以你的javascript看起来像这样(用伪代码)
get user location; send service request for user locations and get them back in an array remove current markers add new markers from service result
PHP中的服务可能如下所示:
$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();希望这能有所帮助。
https://stackoverflow.com/questions/45428306
复制相似问题