Reputation: 65
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.
Upvotes: 2
Views: 1395
Reputation: 6106
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;
?>
Upvotes: 5