0

The code below gets the coordinates of an address/location using Google's Geocoding API, It works manually typing an address where I have written "ADDRESS GOES HERE" below, doing so then outputs the coordinates appropriately.

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

}

$coords = getCoordinates("ADDRESS GOES HERE ");
$coords = join(',', $coords);

?>

What id now like to do now, is use custom fields (ACF) to call the addresses dynamically into "ADDRESS GOES HERE".

The fields that contain the addresses are currently called into a template currently like this :

<?php if( get_field('address_line_1') ): ?><?php the_title(); ?>, <?php the_field('address_line_1', $post_id); ?>,<?php the_field('address_line_2', $post_id); ?>, <?php the_field('town', $post_id); ?>, <?php the_field('post_code', $post_id); ?>, <?php the_field('country', $post_id); ?><?php endif; ?>

Whats the best way to use the above field in the "Address goes here" location ?

For reference the curl script is a spin off of https://colinyeoh.wordpress.com/2013/02/12/simple-php-function-to-get-coordinates-from-address-through-google-services/, which didnt seem to work as effectively.

3
  • get_field is not a WordPress function. where does it come from ?
    – mmm
    Oct 22, 2017 at 11:43
  • Sorry I should have said, Its used for ACF ( advanced custom fields )
    – Randomer11
    Oct 22, 2017 at 11:44
  • Where is getCoordinates() defined/run? You're using curl() directly, which implies that this isn't in a WordPress context? Oct 23, 2017 at 7:09

1 Answer 1

1

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

?>
1
  • Thank you Shital, I actually thought there would be more to it than that. It works perfectly .
    – Randomer11
    Oct 23, 2017 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.