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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分