4

我想计算两点之间的距离。点是地址。

例子:

A点:美国纽约州纽约肯尼迪国际机场

B点:拉瓜迪亚,纽约,纽约,美国

现在我想计算 A 点和 B 点之间的距离(通过道路)和旅行时间。

我怎样才能做到这一点?是否可以使用谷歌地图 API?你会如何处理这个问题?

4

2 回答 2

20
<?php 
$from = "sr nagar,hyderabad";
$to = "kukatpalle,hyderabad";
$from = urlencode($from);
$to = urlencode($to);
$apiKey= "";  
$data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&key=$apiKey&language=en-EN&sensor=false");
$data = json_decode($data);
$time = 0;
$distance = 0;
foreach($data->rows[0]->elements as $road) {
    $time += $road->duration->value;
    $distance += $road->distance->value;
}
echo "To: ".$data->destination_addresses[0];
echo "<br/>";
echo "From: ".$data->origin_addresses[0];
echo "<br/>";
echo "Time: ".$time." seconds";
echo "<br/>";
echo "Distance: ".$distance." meters";
?>

注意:上面你需要公里和时间 h:m 格式只需替换为

$time      = $road->duration->text;
$distance  = $road->distance->text;
于 2013-11-22T11:50:12.720 回答
0

您可以使用 Google Maps API 和 PHP 来计算两个地址之间的距离。

$addressFrom = 'Insert from address';
$addressTo = 'Insert to address';
$distance = getDistance($addressFrom, $addressTo, "K");
echo $distance;

getDistance() 函数可以从这里找到 - http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/

于 2015-07-18T09:32:17.843 回答