Skip Navigation

[Resolved] Extracting Longitude, Latitude from Maps Field

This thread is resolved. Here is a description of the problem and solution.

Problem:

The customer was using the "save_post" hook to automatically fill lat/lon coordinates from a Toolset address type field into another custom field, but the values were not becoming available for new posts.

Solution:

Suggested to get the values from superglobal "$_POST", instead of the custom fields table.
( ref: https://toolset.com/forums/topic/extracting-longitude-latitude-from-maps-field/#post-2257137 )

Relevant Documentation:
n/a

This support ticket is created 2 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#2256573

JEN

Tell us what you are trying to do?

Building a directory site and want to be able to sort results by proximity. In order to do this the location needs to be stored as longitude, latitude. Google Maps already has this info but I'm not sure how to get it and insert into the field. It's in a different array from the rest of the address data. hidden link

Is there any documentation that you are following?

https://toolset.com/forums/topic/extracting-address-data-from-maps-field/
https://toolset.com/forums/topic/split-extracting-address-data-from-maps-field-and-saving-as-taxonomy-term/

Is there a similar example that we can see?

The code from the above links is working well. Need to extract another datapoint in the form of lat,long.

What is the link to your site?
hidden link

This is the missing piece for this site. With this we will be able to sort by proximity.

Thank you!

Jen

#2257109

JEN

I managed to get this working for front end edit form by adapting the code from extracting the state to taxonomy.

This works on edit but not create. Perhaps because I used a shortcode to get lat/long and that value is not yet available on first save. Not sure how else to get the lat/long as that data is in a different array from the address components. I'm sure there is a better way.

// add long/lat
add_action( 'save_post', 'custom_process_longlat_save_post', 10,3 );
function custom_process_longlat_save_post( $post_id, $post, $update ) {
    if ( ('listing' !== $post->post_type) OR ($post->post_status == 'auto-draft') OR (empty($_POST)) ) {
        return;
    }
   
    $post_id = $post->ID;
    if(!empty($_POST['wpcf-street-address'])) {
		$street_location = do_shortcode( '[types field="street-address" format="FIELD_LATITUDE, FIELD_LONGITUDE"][/types]' );
        custom_process_new_longlat($post_id, $street_location);
    }
	
    if(!empty($_POST['wpcf']['street-address'])) {
		$street_location = do_shortcode( '[types field="street-address" format="FIELD_LATITUDE, FIELD_LONGITUDE"][/types]' );
        custom_process_new_longlat($post_id, $street_location);
    }
}
 
function custom_process_new_longlat($post_id, $street_location) {
        update_post_meta( $post_id, 'wpcf-location', $street_location );
}

#2257137

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Your understanding is correct and the use of shortcode to get the custom field value from the table, it too early for new posts, as the value has not been saved in the table yet.

Instead of getting the values from the custom fields table, you can use the superglobal "$_POST".

Example:


// add long/lat
add_action( 'save_post', 'custom_process_longlat_save_post', 10,3 );
function custom_process_longlat_save_post( $post_id, $post, $update ) {
    if ( ('listing' !== $post->post_type) OR ($post->post_status == 'auto-draft') OR (empty($_POST)) ) {
        return;
    }
    
    $post_id = $post->ID;
    if(!empty($_POST['wpcf-street-address'])) {
        // removed line: $street_location = do_shortcode( '[types field="street-address" format="FIELD_LATITUDE, FIELD_LONGITUDE"][/types]' );
        $street_location = $_POST['toolset-extended-form-wpcf-street-address']['latitude'].', '.$_POST['toolset-extended-form-wpcf-street-address']['longitude'];
        custom_process_new_longlat($post_id, $street_location);
    }
     
    if(!empty($_POST['wpcf']['street-address'])) {
        // removed line: $street_location = do_shortcode( '[types field="street-address" format="FIELD_LATITUDE, FIELD_LONGITUDE"][/types]' );
        // removed line: custom_process_new_longlat($post_id, $street_location);
        $street_location = $_POST['toolset-extended-form-wpcf']['street-address']['latitude'].', '.$_POST['toolset-extended-form-wpcf']['street-address']['longitude'];
        $_POST['wpcf']['post-location'] = $street_location;
    }
}
  
function custom_process_new_longlat($post_id, $street_location) {
        update_post_meta( $post_id, 'wpcf-location', $street_location );
}

This should do the trick, for new and/or existing posts.

regards,
Waqar

#2257165

JEN

This works! Thank you.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.