Skip to content

Documentation

Extensive Documentation for PowerPack & WooPack to help you with common questions and issues.

PowerPack

⌘K
  1. Home
  2. Docs
  3. PowerPack
  4. Modules
  5. Google Maps
  6. Fetch Latitude & Longitude from ACF Pro GoogleMap Field?

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.

ACF Pro Google Map Field in Post Editor Window
ACF Pro Google Map Field in Post Editor Window

Creating shortcode

  1. 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

  1. Set the source to ACF Repeater Field or Post (assuming you have posts containing ACF Google Map field)
  2. In the latitude field add the following shortcode [pp_map field="googlemap" sub="lat"]
  3. In the longitude field add the following shortcode [pp_map field="googlemap" sub="lng"]
Using Shortcode in PowerPack Google Map Module
Using Shortcode in PowerPack Google Map Module
×