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