doubiao9775 2013-01-29 19:06
浏览 40
已采纳

两个地点之间的行车距离

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. 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. 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.

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.

  • 写回答

1条回答 默认 最新

  • duanla4959 2013-01-29 19:17
    关注

    It should be almost ok with that, except that the json is in a different format. 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

    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;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏