2017-08-01 99 views
0

目前,我已经创建了一个页面,显示谷歌地图上用户的所有位置(全部来自MySQL数据库)。但是因为有5000多个用户位置,加载该页面需要很长时间。在谷歌地图上查询来自MySQL的位置数据

我想通过仅查询特定区域内的用户位置来提高效率。 (我正在查看的区域)

我能做些什么来提高效率?谷歌地图API是否支持我想要的功能?我听说我可以做geofencing mysql,但我真的找不到如何使用它。

预先感谢您。

enter image description here

+0

什么是你的地图的中心?用户或固定点的当前位置? – Ares

+0

@Ares地图的中心位置是用户正在查看的当前位置。 –

+0

你如何找回JavaScript的位置?你使用什么后端语言? – Ares

回答

2

可能有很多方法可以做你想做的。如果你有用户的经度和纬度(听起来像你这样做),然后代替加载所有用户位置(select latitude, longitude from users),然后通过指定用户坐标的范围来缩小位置,所以你的选择看起来像这样(在伪查询)

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(); 

希望有所帮助。

+0

感谢您的建议^^ –