The issue turned out to be a flaw in my logic. A 200 response code does not mean things are good. It simply means we got a response. Digging deeper showed that when the address could not be found by Google a code was returned showing that the address could not be found.
This code is a bit clumsy, but it works.
//* Function to retrieve & store latitude and longitude of an address field using Google Maps api
function my_save_data_action($post_id, $form_data)
{
if ($form_data['id']==629 || $form_data['id']==1916) {
$latitude_test = get_post_meta($post_id, 'wpcf-latitude', true);
$longitude_test = get_post_meta($post_id, 'wpcf-longitude', true);
if (empty($latitude_test)){
//echo 'Empty Latitude value is ';
//echo $latitude_test;
$custom_fields_address = get_post_meta($post_id, 'wpcf-property-address1', true);
$custom_fields_city = get_post_meta($post_id, 'wpcf-city', true);
$custom_fields_state = get_post_meta($post_id, 'wpcf-state', true);
$custom_fields_country = 'United States';
$custom_fields = ($custom_fields_address.' '.$custom_fields_city.' '.$custom_fields_state.' '.$custom_fields_country);
// echo"Custom fields are $custom_fields ";
$passed_url= urlencode($custom_fields);
// echo 'passed url is ';
// echo $passed_url;
if(!empty($custom_fields))
{
$resp = wp_remote_get('hidden link'.urlencode($custom_fields).'&sensor=false');
echo 'response code is ';
echo $resp['response']['code'];
//die();
if (!200 == $resp['response']['code'] ) {
echo "bad response";
die();
}
if ( 200 == $resp['response']['code'] ) {
$body = $resp['body'];
$data = json_decode($body);
if($data->status=='OK'){
echo "status is ";
echo $data->status;
$latitude = $data->results[0]->geometry->location->lat;
$longitude = $data->results[0]->geometry->location->lng;
echo " Latitude is ";
echo $latitude;
echo $longitude;
//die();
/*response code is 200OK34.5265429-86.69505 die(); */
update_post_meta($post_id, 'wpcf-latitude',$latitude,$latitude_test);
update_post_meta($post_id, 'wpcf-longitude',$longitude,$longitude_test);
}else{
echo " data status is not OK ";
echo $data->status;
// die();
} //end of data status is OK
} //end of test for 200 response code
}// not empty custom fields
}else{
// Latitude already has a value
echo 'Not Empty Show Latitude value is ';
echo $latitude_test;
// die();
} //End of else test for value
}else{
echo "neither form was detected.";
// die();
}//end of form test die if not the form we seek
} //end of function
add_action('cred_save_data', 'my_save_data_action',10,2);