Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code to look up the longitude and latitude based off of a custom field upon saving the post. My address fields (street_1, city and state) are separate. I have searched and thought that I could put the values from these separate meta keys into an array and pass them to the URL. I have the state value working but I need the whole address. I have tried many different ways and am now asking for help. Any clues will be appreciated.

Here is the code:

function geocode_address($post_id)
{
    $custom_fields = get_post_custom();
    if(isset($custom_fields['state']) && !empty($custom_fields['state'][0]))
    {
 $resp = wp_remote_get( "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($custom_fields['state'][0])."&sensor=false" );
            if ( 200 == $resp['response']['code'] ) {
                    $body = $resp['body'];
                    $data = json_decode($body);
                    if($data->status=="OK"){
                            $latitude = $data->results[0]->geometry->location->lat;
                            $longitude = $data->results[0]->geometry->location->lng;
                            update_post_meta($post_id, "latitude", $latitude);
                            update_post_meta($post_id, "longitude", $longitude);
                    }
            }
    }
}
add_action('save_post', 'geocode_address');

Thank you, Deon

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

There is no need to put them into an array. According to google's documentation you should just separate out the various pieces of the address in the url like a standard (U.S.) formatted address.

// Example taken from google's docs:
$state = 'CA';
$street = 'Mountain View';
$address = '1600 Amphitheatre Parkway';

function geocode_address($post_id)
{
    $custom_fields = get_post_custom();

    // just set $city, $state and $address to the appropriate custom field variables here

    if(isset($custom_fields['state']) && !empty($custom_fields['state'][0]))
    {

 $resp = wp_remote_get( "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($address . ',' . $city . ',' .$state)."&sensor=false" );
            if ( 200 == $resp['response']['code'] ) {
                    $body = $resp['body'];
                    $data = json_decode($body);
                    if($data->status=="OK"){
                            $latitude = $data->results[0]->geometry->location->lat;
                            $longitude = $data->results[0]->geometry->location->lng;
                            update_post_meta($post_id, "latitude", $latitude);
                            update_post_meta($post_id, "longitude", $longitude);
                    }
            }
    }
}
add_action('save_post', 'geocode_address');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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