ACF Google Maps – Ways to output and format the Address in text form.

You can use Advanced Custom Fields to do some pretty cool things with Google Maps. But often you would want to spit out the address from the map in a formatted form. Here are some examples:

Simplest form:

“111 My Road, City, ST, United States”

// Get your field
$address = get_field('address');
// Get the Address String
echo $address['address'];

Strip the “, United States”

“111 My Road, City, ST”

// Get your field
$address = get_field('address');
// Get the Address String
$location = $address['address'];
// Find the "US" at the end of the string
$us = ', United States';
// And drop if from the Address
$trimmedAdd = str_replace($us, '', $location);
// spit it out
echo $trimmedAdd;

Strip the “, United States” and add a break making it multiline

” 111 My Road
City, ST ”

// Get your field
$address = get_field('address');
// Get the Address String
$location = $address['address'];
// Find the first comma and replace it with a break
$string = strpos($location,',');
if ($pos !== false) {
	$newstring = substr_replace($location,'<br>',$string,strlen(','));
}
// Find the "US" at the end of the string
$us = ', United States';
// And drop if from the Address
$trimmedAdd = str_replace($us, '', $newstring);
// spit it out
echo $trimmedAdd;