Insert Address fields into function
<?php
function getCoordinates($address) {
$address = urlencode($address);
// set HTTP header
$headers = array('Content-Type: application/json');
// Open connection
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://maps.google.com/maps/api/geocode/json?sensor=false&address=" . $address);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// Execute request
$result = curl_exec($curl);
// Close connection
curl_close($curl);
// get the result and parse to JSON
$json = json_decode($result);
$lat = $json->results[0]->geometry->location->lat;
$lng = $json->results[0]->geometry->location->lng;
return array($lat, $lng);
}
$post_id = get_the_ID();
$title = get_the_title($post_id );
$address_line_1 = get_field( "address_line_1",$post_id );
$address_line_2 = get_field( "address_line_2",$post_id );
$town = get_field( "town",$post_id );
$post_code = get_field( "post_code",$post_id );
$country = get_field( "country",$post_id );
if($address_line_1!="")
{
$address_string = $title.",".$address_line_2.",".$town.",".$post_code.",".$country;
}
else
{
$address_string = "";
}
$coords = getCoordinates($address_string);
$coords = join(",', $coords);
?>