谷歌地图中的路线重叠

时间:2017-03-08 10:38:57

标签: php google-maps

问题 - >我需要找出两条路线之间的重叠百分比。

到目前为止尝试了解决方案 - >我尝试将原点和目的地(当然还有密钥)传递给以下网址

https://maps.googleapis.com/maps/api/directions/json

并相应地解析了json响应。这是我的代码片段 -

$endpoint = 'https://maps.googleapis.com/maps/api/directions/json?origin='.$startLatitude1.','.$startLongitude1.'&destination='.$endLatitude1.','.$endLongitude1.'&key='.$mykey;
$json1 = file_get_contents($endpoint.http_build_query(array())); //array is empty here
$data1 = json_decode($json1);

if ($data1->status === 'OK') {

    $endpoint = 'https://maps.googleapis.com/maps/api/directions/json?origin='.$startLatitude2.','.$startLongitude2.'&destination='.$endLatitude2.','.$endLongitude2.'&key='.$mykey;
    $json2 = file_get_contents($endpoint.http_build_query(array())); //array is empty here
    $data2 = json_decode($json2);

    $polyline = array();

    if ($data2->status === 'OK') {
        $route2 = $data2->routes[0];
        foreach ($route2->legs as $leg2) {
            foreach ($leg2->steps as $step2) {
                $polyline[$step2->polyline->points] = $step2->distance->value;
                }
            }
        }

        $overlap = 0;
        $totalDistance = 0;
        $route1 = $data1->routes[0];
        foreach ($route1->legs as $leg1) {
        $totalDistance = $leg1->distance->value;
            foreach ($leg1->steps as $step1) {
                if (array_key_exists($step1->polyline->points, $polyline)) {
                    $overlap = $overlap + $step1->distance->value;
                }
            }
        }
        echo 'Total Distance -> '.$totalDistance;
        echo 'Overlap -> '.$overlap.'<br>';

}

因此,我们首先遍历路径1并将折线存储为以距离为值的关联数组中的键。接下来,我们遍历路径2并检查路由2中的折线是否已存在于先前创建的关联数组中。

问题 - &gt;除非道路是直的,否则这种情况一直有效。假设有4个点 - A,B,C,D,并且所有点都按此顺序排成一行。人X想要从A到D而人Y想要从B到C.所以B-C有重叠。但由于折线永远不会匹配(X&amp; Y的原点和目的地不同),我的代码不会检测到任何重叠。

还有其他出路吗?

0 个答案:

没有答案