$url =
"http://maps.googleapis.com/maps/api/distancematrix/json?origins=144216&
destinations=160017&mode=driving&language=en-EN&sensor=false";
$data = @file_get_contents($url);
$result = json_decode($data, true); //print_r($result); //outputs the array $distances = array( // converts the units
"meters" => $result["rows"][0]["elements"][0]["distance"]["value"],
"kilometers" => $result["rows"][0]["elements"][0]["distance"]["value"] / 1000,
"yards" => $result["rows"][0]["elements"][0]["distance"]["value"] * 1.0936133,
"miles" => $result["rows"][0]["elements"][0]["distance"]["value"] * 0.000621371 );
print_r($distances);
显示此输出:
Array (
[meters] => 5497949
[kilometers] => 5497.949
[yards] => 6012630.1491217
[miles] => 3416.266068079
)
这是不正确的。请让我知道任何解决方案
答案 0 :(得分:1)
如果您查看谷歌的结果,您会发现它正在计算从印度到俄罗斯的路线。
"destination_addresses" : [ "Wologda, Oblast Wologda, Russland, 160017" ],
"origin_addresses" : [ "Punjab 144216, Indien" ],
这是因为您只提供了邮政编码,而没有参考他们所在的国家/地区。我假设您想要计算印度境内的路线,因为您的个人资料显示您来自那里。
如果您只是将,india
添加到邮政编码中,请执行以下操作:
http://maps.googleapis.com/maps/api/distancematrix/json?origins=144216,india&destinations=160017,india&mode=driving&language=en-EN&sensor=false
您得到的结果看起来更像您想要的结果:
{
"destination_addresses" : [ "Chandigarh, 160017, India" ],
"origin_addresses" : [ "Punjab 144216, India" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "201 km",
"value" : 200693
},
"duration" : {
"text" : "3 hours 30 mins",
"value" : 12625
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}