dsfg3241 2015-05-26 10:52
浏览 60

使用php中的纬度和经度计算两点之间的道路距离

Below is the code I have used

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else {
        return $miles;
    }
}

But it provides distance on fly and not by road. For ex: The function returns 2km as answer between two points but when checked the actual distance between those two area on google it returns 10km which is right.

  • 写回答

1条回答 默认 最新

  • dongzaliang4492 2015-06-11 13:05
    关注

    Using google api I could able to find distance between two points. Just need to create http request URL and all done

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, 
            'http://maps.googleapis.com/maps/api/directions/json?origin=add_source&destination=add_destination&sensor=false'
        );
        $content = curl_exec($ch);
        echo $content;
    

    It returns json response which also includes distance.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效