【发布时间】:2015-02-10 06:35:06
【问题描述】:
我现在正在做一个项目,该项目需要计算两点之间的距离(纬度和经度或特定的地名)。
现在,由于我的起点和终点来自我的数据库,我需要使用一个变量来存储起点和终点并最终计算。
我的代码是这样的:
<?php
//request the directions
$origin="maramag";
$desti="malaybalay";
$routes=json_decode(file_get_contents('http://maps.googleapis.com/maps/api/directions/json?origin=$origin&destination=$desti&alternatives=true&sensor=false'))- >routes;
//sort the routes based on the distance
usort($routes,create_function('$a,$b','return intval($a->legs[0]->distance->value) - intval($b->legs[0]->distance->value);'));
//print the shortest distance
echo $routes[0]->legs[0]->distance->text;
echo $routes[0]->legs[0]->duration->text;
?>
但是当我尝试在不使用变量的情况下运行 URL 时,例如:
$routes=json_decode(file_get_contents('http://maps.googleapis.com/maps/api/directions/json?origin=maramag&destination=malaybalay&alternatives=true&sensor=false'))->routes;
它给了我“49.0 km 45 mins”的结果,这是我想要出现的结果。
谁能帮我格式化我的变量?我认为是我的 URL 中包含的变量的格式或语法有问题。
我浏览了谷歌和这个网站,但我从未找到解决问题的方法。
非常感谢!
【问题讨论】:
标签: php google-maps