2

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.

Boaz
  • 19,892
  • 8
  • 62
  • 70
Lubblobba
  • 65
  • 4
  • 14
  • removing the error suppressing @ would be a start –  Jan 29 '13 at 19:08
  • `{"name":"from:London to:Dover","Status":{"code":604,"request":"directions"}}` is what that URL returns for me...not sure how you are going to calculate a distance from that. – crush Jan 29 '13 at 19:10
  • possible solution http://stackoverflow.com/questions/14254641/mysql-query-select-nearest-places-by-a-given-coordinates/14255279#14255279 – mamdouh alramadan Jan 29 '13 at 19:12
  • well i have no idea, i've just been trying different examples online. – Lubblobba Jan 29 '13 at 19:12
  • @crush: Strange, I get `"code":200` and a JSON formatted string containing `"Distance":{"meters":4217…`. – Marcel Korpel Jan 29 '13 at 19:13
  • I get a valid JSON response with coordinates. *shrugs* Gonan need more – James Webster Jan 29 '13 at 19:13
  • i need to find the driving distance between two points :/ – Lubblobba Jan 29 '13 at 19:13
  • The JSON object actually contains all relevant information, including distance among other things – Boaz Jan 29 '13 at 19:14
  • @Lubblobba - do you have the latitude and longitude of the two points – mamdouh alramadan Jan 29 '13 at 19:15
  • well i can put them in sure – Lubblobba Jan 29 '13 at 19:15
  • i still get a blank page – Lubblobba Jan 29 '13 at 19:16
  • @Lubblobba Have you checked if `file_get_contents()` actually succeeds? If it fails, it will simply return `false` and no error would be displayed. – Boaz Jan 29 '13 at 19:25
  • i placed var_dump after each line, and the first 2 contained the data, but var_dump($obj) returns NULL, so the json_decode doesnt appear to work? – Lubblobba Jan 29 '13 at 19:28
  • @Lubblobba Just to make sure - `var_dump($data)` actually displays the raw JSON? – Boaz Jan 29 '13 at 19:30
  • Why am I getting the code 604....wtf – crush Jan 29 '13 at 19:31
  • i have no idea, it contains a lot of stuff, this is a bit of it: string(5876) "{"name":"from:51.519894,-0.105667 to:51.129079,1.306925","Status":{"code":200,"request":"directions"}"Placemark":[{"id":"","address":"Farringdon Rd\/A201","AddressDetails":{"Thoroughfare":{"ThoroughfareName":"Farringdon Rd\/A201"} and it continues – Lubblobba Jan 29 '13 at 19:31
  • The actual problem is in `json_decode`: [`json_last_error`](http://php.net/json_last_error) returns `JSON_ERROR_UTF8`... – Marcel Korpel Jan 29 '13 at 19:34
  • 1
    @Lubblobba This means the request is successful and you actually get data back. It also means `json_decode()` fails. You can check for the possible failure with `echo json_last_error();` – Boaz Jan 29 '13 at 19:35
  • @Lubblobba harpax's updated answer would prevent the error in the JSON decoding. – Boaz Jan 29 '13 at 19:41

1 Answers1

5

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;
?>
harpax
  • 5,986
  • 5
  • 35
  • 49