Fetch Latitude & Longitude from ACF Pro GoogleMap Field?
ACF Pro provides a GoogleMap field which makes it easy to add locations to the posts via a nice graphical interface. You can easily fetch the latitude, longitude, and name data from this custom field by creating a custom shortcode and using it in the Google Map module.
Creating shortcode
- Add the following code to the functions.php file of the active theme.
add_shortcode( 'pp_map' , function( $atts ) {
if (!function_exists('get_field') ) return '';
$atts = shortcode_atts( array( 'field' => false , 'sub' => 'address' ) , $atts, $shortcode = 'pp_map' );
if ($atts[ 'field' ] && $map = get_field( $atts[ 'field'] ) ) {
if ( isset( $map[ $atts['sub'] ] ) ) return $map[ $atts['sub'] ];
}
return '';
} );
Using shortcode to fetch Latitude and Longitude
The shortcode that we have created above can be used to fetch the following fields:
- lat
- lng
- zoom
- place_id
- street_number
- street_name
- street_name_short
- city
- state
- state_short
- post_code
- country
- country_short
For example, if the ACF Pro Google Map field name is googlemap
then a shortcode can be used like this to fetch the latitude.
[pp_map field="googlemap" sub="lat"]
Using the Shortcode in PowerPack Google Map Module
- Set the source to ACF Repeater Field or Post (assuming you have posts containing ACF Google Map field)
- In the latitude field add the following shortcode
[pp_map field="googlemap" sub="lat"]
- In the longitude field add the following shortcode
[pp_map field="googlemap" sub="lng"]