对 google 地图 API 的结果和我的距离矩阵公式感到困惑
Confused with results with google maps API and my formula with distance matrix
我有一个项目会根据我设置的半径检测附近的 markers/establishments。我有 5km/3km/1km
个选项。我暂时将中心设置为静态 lat
和 lng
。在获取标记的坐标时,我有一个数据库用于计算坐标。我使用了一些公式 found.
这是我在后台根据中心标记获取附近标记的代码
Static lat lng from frontend // lat: 1.3182001528756,lng: 103.84650707245
$km = $request->radius/1000; //1km
$centerLat = $request->centerLat;
$centerLng = $request->centerLng;
$ky = 40000 / 360;
$originParm = $centerLat.','.$centerLng;
$test = \DB::select(\DB::raw("SELECT school_name as name, lng, lat,
( 6371 * acos( cos( radians(" . $centerLat . ") )
* cos( radians( lat ) ) * cos( radians( lng ) - radians(" . $centerLng . ") )
+ sin( radians(" . $centerLat . ") ) * sin( radians( lat ) ) ) )
AS distance FROM xp_pn_schools HAVING distance < ".$km.""));
$test = json_decode(collect($test),true);
foreach($test as $data) {
$arr['data'][] = array(
'coord' => ['lat' => $data['lat'], 'lng' => $data['lng']],
'name' => ucwords(strtolower($data['name'])),
'distance' => $data['distance'],
);
$coordArr[] = $data['lat'].','.$data['lng']; //concat for google api link
}
使用古尔距离矩阵的后端api(根据位置获取预计到达时间和距离)
$destination = implode('|',$coordArr);
$ch = curl_init();
$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins='.$originParm.'&destinations='.rawurlencode($destination).'&key=***********&mode=driving';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$output = json_decode($output,true);
curl_close($ch);
return response()->json(array(
'markers' => $arr,
'time' => $output
));
但是在我的前端,当我检查响应时,我得到了类似
的东西
我将前端的半径设置为 1 公里或 1000 公里,以便呈现很少的结果进行比较
如您所见,第一个结果 Farrer School
与公式的距离为 0.8 km
。但在 google 地图 api 中是 2.1 km
。我如何验证两者中哪一个是正确的?
haversine公式给出很大的circle/straight线距离,距离矩阵给出行车距离。这两者几乎总是不同的,行驶距离是两者中较大的一个。
我有一个项目会根据我设置的半径检测附近的 markers/establishments。我有 5km/3km/1km
个选项。我暂时将中心设置为静态 lat
和 lng
。在获取标记的坐标时,我有一个数据库用于计算坐标。我使用了一些公式 found.
这是我在后台根据中心标记获取附近标记的代码
Static lat lng from frontend // lat: 1.3182001528756,lng: 103.84650707245
$km = $request->radius/1000; //1km
$centerLat = $request->centerLat;
$centerLng = $request->centerLng;
$ky = 40000 / 360;
$originParm = $centerLat.','.$centerLng;
$test = \DB::select(\DB::raw("SELECT school_name as name, lng, lat,
( 6371 * acos( cos( radians(" . $centerLat . ") )
* cos( radians( lat ) ) * cos( radians( lng ) - radians(" . $centerLng . ") )
+ sin( radians(" . $centerLat . ") ) * sin( radians( lat ) ) ) )
AS distance FROM xp_pn_schools HAVING distance < ".$km.""));
$test = json_decode(collect($test),true);
foreach($test as $data) {
$arr['data'][] = array(
'coord' => ['lat' => $data['lat'], 'lng' => $data['lng']],
'name' => ucwords(strtolower($data['name'])),
'distance' => $data['distance'],
);
$coordArr[] = $data['lat'].','.$data['lng']; //concat for google api link
}
使用古尔距离矩阵的后端api(根据位置获取预计到达时间和距离)
$destination = implode('|',$coordArr);
$ch = curl_init();
$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins='.$originParm.'&destinations='.rawurlencode($destination).'&key=***********&mode=driving';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$output = json_decode($output,true);
curl_close($ch);
return response()->json(array(
'markers' => $arr,
'time' => $output
));
但是在我的前端,当我检查响应时,我得到了类似
的东西我将前端的半径设置为 1 公里或 1000 公里,以便呈现很少的结果进行比较
如您所见,第一个结果 Farrer School
与公式的距离为 0.8 km
。但在 google 地图 api 中是 2.1 km
。我如何验证两者中哪一个是正确的?
haversine公式给出很大的circle/straight线距离,距离矩阵给出行车距离。这两者几乎总是不同的,行驶距离是两者中较大的一个。