0

I am working on a project now which needs to calculate distance between two point (either a latitude and longitude or a specific name of place).

Now, since my origin and destination comes from my database, i need to use a variable where the origin and destination be stored and eventually computed.

My code goes like this:

<?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;
?>

But when I tried running the URL without using variables, such as this:

    $routes=json_decode(file_get_contents('http://maps.googleapis.com/maps/api/directions/json?origin=maramag&destination=malaybalay&alternatives=true&sensor=false'))->routes;

It gives me a result of "49.0 km 45 mins" which is the result I wanted to appear.

Can anybody help me format my variable? I think it's the format or syntax of my variable contained in the URL has problem.

I browsed Google and on this site but I had never found a solution for my problem.

Thank you very much!

JeffMan
  • 412
  • 4
  • 11

3 Answers3

0

Use the string concatenation operator to create your URL:

$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;

If the strings contain spaces or other characters that are not legal in URLs, URL encode them first.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Thank you very much @geocodezip for your answer! It has helped me a lot. But there is lacking with that if the origin and destination contains spaces, it will send an error. Anyway your answer has helped me and we have solved this problem. THANKS a lot! – JeffMan Feb 10 '15 at 11:28
0

The answer of @geocodezip above is right but it will trigger an error if the Origin and Destination contains spaces. So we must replace the spaces with "+" to avoid it. The complete answer to this question is below:

$origin="Dologon, Maramag, Bukidnon";
$desti="Malaybalay City, Bukidnon";

//replace the spaces with "+"
$address1 = str_replace(' ', '+', $origin);
$address2 = str_replace(' ', '+', $desti);

//use concatenation as answered by @geocodezip
$routes=json_decode(file_get_contents('http://maps.googleapis.com/maps/api/directions/json?origin='.$address1.'&destination='.$address2.'&alternatives=true&sensor=false'))->routes;

So instead of making our origin=$origin and our destination=#desti, we must make it origin='.$origin.' and for destination we must make it destination='.$desti.'

Happy coding! Thank you very much for helping!

JeffMan
  • 412
  • 4
  • 11
0

Using your code I was able to get a proper response with an address containing spaces by using urlencode and making sure that I used double quotes instead of single quotes.

Here is all I did:

$location = urlencode($location); // May contain spaces
$routes = json_decode(file_get_contents("http://maps.googleapis.com/maps/api/directions/json?origin=$userLat,$userLong&destination=$location&alternatives=true&sensor=false"))->routes;

The rest of your code worked perfectly and returns the correct distance and travel time. I had no need to use PHP's string concatenation syntax (ie: "Text ". $var ." more text)

In case anyone is curious, I got the user's latitude and longitude via javascript/PHP using the answer from this post

Community
  • 1
  • 1
Ponyboy47
  • 924
  • 9
  • 15