Elevating Your Location Data: A Guide to Storing Latitude and Longitude with ACF Google Map Field

Latitude and Longitude with ACF Google Map

Introduction: Latitude and Longitude with ACF Google Map

In this technical blog post, we’ll not only explore the importance of storing latitude and longitude as separate fields alongside ACF Google Map data but also provide you with the code snippets to seamlessly save this information. Join us on this journey to enhance your location-based queries and improve the efficiency of your WordPress site.

Storing Latitude and Longitude Data:

Before we dive into the code, let’s make sure we have separate latitude and longitude fields within the ACF field group.

  1. Modify ACF Field Group: Open your ACF field group and ensure you have added two additional fields named latitude and longitude.
   add_action('acf/init', 'my_acf_init');
   function my_acf_init() {
      acf_add_local_field_group(array(
         // Field group settings...
         'fields' => array(
            array(
               'key' => 'field_latitude',
               'label' => 'Latitude',
               'name' => 'latitude',
               'type' => 'number',
               // Additional settings...
            ),
            array(
               'key' => 'field_longitude',
               'label' => 'Longitude',
               'name' => 'longitude',
               'type' => 'number',
               // Additional settings...
            ),
            array(
               'key' => 'field_location',
               'label' => 'Location',
               'name' => 'location',
               'type' => 'google_map',
               // Additional settings...
            ),
            // Other fields...
         ),
      ));
   }

Saving Latitude and Longitude Data:

Now, let’s add the necessary code to automatically save the latitude and longitude values when a user selects a location on the Google Map field.

  1. Hook into ACF Save Post Action:
   add_action('acf/save_post', 'save_latitude_longitude', 20);

   function save_latitude_longitude($post_id) {
      // Ensure this is not an autosave or a post revision.
      if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
      if (wp_is_post_revision($post_id)) return;

      // Check if the post type is the one associated with your ACF field group.
      if (get_post_type($post_id) === 'your_custom_post_type') {
         // Get the location data from the Google Map field.
         $location = get_field('location', $post_id);

         // Save latitude and longitude to their respective fields.
         if ($location) {
            update_field('latitude', $location['lat'], $post_id);
            update_field('longitude', $location['lng'], $post_id);
         }
      }
   }

Replace 'your_custom_post_type' with the actual post type associated with your ACF field group.

Conclusion: Latitude and Longitude with ACF Google Map

By incorporating separate latitude and longitude fields alongside the ACF Google Map field and implementing the provided code snippets, you can effortlessly enhance your WordPress site’s ability to store and retrieve location data. This approach not only optimizes location-based queries but also streamlines the process of working with geospatial information in your applications.

If you need help doing radius searches on ACF Google map data get in touch.

Photo by Dariusz Sankowski on Unsplash

Get A No Obligation Quote

Do You Need Help With Your WooCommerce Site?

Click through to the next page and complete the form to get a free no obligation quote to fix any issue you are having with your WooCommerce site.