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!