to calculate driving distance between two coordinates on earth in php

╄→гoц情女王★ 提交于 2019-12-08 02:41:50

问题


Well i am working in a location based app and i need to find driving distance between two location on earth at the server end. i am coding the web service in php.

Till now i am able to calculate the aerial distance using the Haversine formula, but now i want to calculate the driving distance.

There can be possible duplicates for this question, but i was not able to find any help to calculate the "Driving distance" between two "coordinates" ie latitude and longitude instead of addresses in PHP


回答1:


This should get you started...

$json = file_get_contents('http://maps.google.com/maps/nav?q=from:Buderim,Australia%20to:Brisbane,Australia');

$details = json_decode($json, TRUE);

var_dump($details['Directions']['Distance']['meters']);



回答2:


Use Google Distance Matrix API :-

The Google Distance Matrix API is a service that provides travel distance and time for a matrix of origins and destinations.

Here is the code:-

$q = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&mode=driving&sensor=false";

$json = file_get_contents($q);

$details = json_decode($json, TRUE);

echo "<pre>"; print_r($details); echo "</pre>";

Find out more at Google Distance Matrix API



来源:https://stackoverflow.com/questions/6211127/to-calculate-driving-distance-between-two-coordinates-on-earth-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!