简体   繁体   English

2个地点之间的行驶距离

[英]Driving distance between 2 locations

There are plenty of pages about this but I've yet to find something useful that actually works. 有很多关于此的页面,但是我还没有找到真正有用的有用的东西。 Currently, I have used the Google Distance Matrix and found the distance between two locations, however had no luck transferring the distance from JavaScript to PHP. 目前,我已经使用Google距离矩阵并找到了两个位置之间的距离,但是没有从Java到PHP的距离。 I made a post on here yesterday about that with no success. 我昨天在这里发表了一篇关于此事的文章,但没有成功。 So the other option I have is to find the distance using the matrix in PHP. 因此,我的另一个选择是使用PHP中的矩阵查找距离。 I'll still show the map and everything, just on the next page I'd like to get the distance value between the two locations to use it to find a cost for a trip that is then sent to mysql database, so I need to get that value to PHP. 我仍将显示地图和所有内容,仅在下一页上,我想获取两个位置之间的距离值,以使用它来查找行程的成本,然后将其发送到mysql数据库,因此我需要获得该值到PHP。

I've tried various examples I've found online but none seem to work for me. 我尝试了在网上找到的各种示例,但似乎没有一个适合我。

$url = 'http://maps.google.com/maps/nav?q=from:London%20to:Dover';
$data = @file_get_contents($url);
$obj = json_decode($data);
print $obj->meters;

For example that. 例如那个。 I have no idea how to do what I'm trying to, regardless of searching for days on end and would really appreciate some help as I've been stuck on this for a while. 我不知道如何做自己想做的事情,无论最终要搜索多少天,而且由于我在此问题上停留了一段时间,我将不胜感激。

It should be almost ok with that, except that the json is in a different format. 除了json格式不同外,几乎应该可以。 The last line of your example should be: 您的示例的最后一行应该是:

echo $obj->Directions->Distance->meters;

for more infos on the format of the answer try a var_dump in php .. Hope that helps 有关答案格式的更多信息,请尝试在php中使用var_dump。

EDIT: 编辑:

<?php
    $url = 'http://maps.google.com/maps/nav?q=from:London%20to:Dover';
    $data = @file_get_contents($url);
    $data = utf8_decode($data);
    $obj = json_decode($data);
    echo $obj->Directions->Distance->meters;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM