如何计算两个地址之间的距离 [英] How to calculate the distance between two addresses

查看:933
本文介绍了如何计算两个地址之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如:

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

点B:拉瓜地亚,纽约州,纽约州,美国

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

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


解决方案

 < PHP
从$ =SR格尔,海得拉巴
$到=库卡特帕尔莱,海得拉巴
从$ = urlen code($来自);
$为= urlen code($到);
$数据= file_get_contents(\"http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false\");
$数据= json_de code($的数据);
$时间= 0;
$距离= 0;
的foreach($数据 - >行[0] - >元素$路){
    $ +时间= $公路 - > duration->价值;
    $距离+ = $公路 - >距离 - >价值;
}
回声若要:数据 - $> destination_addresses [0];
回声< BR />中;
回声从:$数据 - > origin_addresses [0];
回声< BR />中;
回声时间:$时间秒。
回声< BR />中;
回声距离:$距离米。
?>

注:上面你需要公里,时间H:M格式只是

更换

  $时间= $公路 - > duration->文字;
$距离= $公路 - >距离 - >文字;

I would like to calculate the distance between two points. The points are addresses.

Example:

Point A: JFK Airport, New York, NY, United States

Point B: La Guardia, New York, NY, United States

Now I want to calculate the distance (via roads) and the travel time between point A and point B.

How can I do that? Is it possible to use google maps API? How would you approach the problem?

解决方案

<?php 
$from = "sr nagar,hyderabad";
$to = "kukatpalle,hyderabad";
$from = urlencode($from);
$to = urlencode($to);
$data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&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";
?>

Note : above you need to km and time h:m format just replace with

$time      = $road->duration->text;
$distance  = $road->distance->text;

这篇关于如何计算两个地址之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆