I have a custom post type for addresses on a Google Map. One meta field is for the address, but I want to convert that address to longitude/latitude. So I have a meta field for longitude & one for latitude.
I created a function in my loop to update the longitude & latitude postmeta based on the address postmeta. Here is the function:
function geocode_spot_address ($post_ID) {
$address = get_post_meta($post_ID,'spots_address',true);
$address = str_replace(' ', '+', $address);
$address = 'http://maps.google.com/maps/geo?q=1020+' . $address . '&output=xml';
$result = file_get_contents($address);
$xml = new SimpleXMLElement($result);
list($longitude, $latitude, $altitude) = explode(",",$xml->Response->Placemark->Point->coordinates);
update_post_meta($post_ID, 'spots_longitude', $longitude);
update_post_meta($post_ID, 'spots_latitude', $latitude);
return $post_ID;
}
This works great when I refresh the page that I call the function on in the loop and pass the post ID:
geocode_spot_address($post->ID);
For the life of me though, I can’t get this to work when trying to get it to update longitude/latitude on post save/publish/update by hooking the function to:
add_action('save_post', 'geocode_spot_address', 1, 2);
I obviously just want to enter the street address, then on save/publish/update have it run this function and add longitude/latitude to the appropriate meta fields.
Any help would be appreciated.
FWIW I am using Rilwis’s Meta Box framework for creating my custom post types and meta boxes.