I am using this tutorial to calculate the distance between two addresses using Google Maps.
I am looking to output the distance between the origin address, which is a fixed point, and the destination address, which is dependent on what the user inputs in the _ct_text_53ea3270e6e6d field.
It works fine pulling from one address, but if it pulls from multiple maps, it only shows the distance from one of the maps, not from each of them.
Here is a screenshot showing the problem. In the screenshot, it is successfully pulling the distance from the first address, which is within very close proximity of the origin address. However, the second result has an address that is ~10 miles away, but it is not grabbing that address and only repeats the first result's output.
Here is the code I'm using to get this distance:
<?php
$field_value = do_shortcode('[ct id="_ct_text_53ea3270e6e6d" property="title | description | value"]');
// Our parameters
$params = array(
'origin' => '900 South Crouse Ave, Syracuse, NY, 13210',
'destination' => $field_value,
'sensor' => 'true',
'units' => 'imperial'
);
// Join parameters into URL string
foreach($params as $var => $val){
$params_string .= '&' . $var . '=' . urlencode($val);
}
// Request URL
$url = "http://maps.googleapis.com/maps/api/directions/json?".ltrim($params_string, '&');
// Make our API request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($curl);
curl_close($curl);
// Parse the JSON response
$directions = json_decode($return);
?>
<strong>Distance from Campus</strong>:
<?php
// Print distance
printf($directions->routes[0]->legs[0]->distance->text);
?>
I just need a second look to see why this happening.