Skip Navigation

[Resolved] How to retrieve the lat/lon coordinates for an address?

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

Problem:
The client wants to use the CRED API to save the lat/lon coordinates of a post that contains an address field as well as the text address itself.

Solution:
Views caches the coordinates alongside the address string in wp_options and they can be retrieved using the WP function get_option. The format is described below: https://toolset.com/forums/topic/store-lat-long-coordinates-at-cred-submission/#post-902441

Relevant Documentation:
https://developer.wordpress.org/reference/functions/get_option/

This support ticket is created 6 years, 7 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by gavinS-2 6 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#902392

Hi

I have two custom post types, which each have a Google Maps address field.

It is my understanding that Toolset only stores a string of the address, and not the Lat / Long coordinates.

I wish to store the Lat Long coordinates in order to allow me to do calculations with them later (distance between two points).

I also believe that the lat / long coordinates are stored in a cache at the time when the address string is stored. So I'm trying to find a way to store those cached coordinates in other fields at the time of CRED submission.

Do I understand correctly that I could create a function to save this data on CRED submission? It seems I could store something like this:

[types field='my-address' format='FIELD_ADDRESS: FIELD_LATITUDE, FIELD_LONGITUDE'][/types]

Which I'm guessing would be an array of the two coordinates?

This thread mentions some possible update that might make this easier (If I'm reading it correctly):

https://toolset.com/forums/topic/storage-location-of-latitudelongitude/page/2/

Is there any news on that possible update which could make this easier?

#902441

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Gavin

Here's how it works.

With a Types address field, when you edit a post and start to enter an address, the Google API auto-suggestion lists matching addresses as you type.

When you chose one Types stores the formatted text address as returned by Google in wp_postmeta, so what is stored as the custom field value is the text address.

Now, to be able to display content on maps and for use with the distance filters, we store the lat and lon (retrieved at the same time as the text address) in a cache in the wp_options table.

The option name is toolset_maps_address_coordinates, and the text address and corresponding coordinates are stored in a serialised array.

You can use get_option to retrieve this (https://developer.wordpress.org/reference/functions/get_option/) and it will look something like this:

Array
(
    [69e644ce2fcb2c6ff42317d0fcf56ae1] => Array
        (
            [lat] => 52.9613336
            [lon] => -6.1486986
            [address] => Ballinacoola, Glenealy, Co. Wicklow, Ireland
            [address_passed] => Ballinameesda Lower, Ballinacoola, Ireland
        )

    [42a171c425c14ae890caf476674b2895] => Array
        (
            [lat] => 48.8570772
            [lon] => 2.3568022
            [address] => 19 Rue Vieille du Temple, 75004 Paris, France
            [address_passed] => 19 Rue Vieille du Temple, Paris, France
        )

    [0979b7ba8969460aad4f28489c4995e4] => Array
        (
            [lat] => 45.5024399
            [lon] => 9.1979888
            [address] => Viale Fulvio Testi, 1, 20159 Milano, Italy
            [address_passed] => Viale Fulvio Testi, 1, Milan, Metropolitan City of Milan, Italy
        )
)

So, for any address (that you get from wp_postmeta) you can look up the corresponding coordinates to use as required.

What precisely is required depends on what exactly you are aiming for, but that is how the data is stored.

#902457

Thanks Nigel

So if I have understood you correctly, the lat / long coordinates are only temporarily stored, and I DO need to save them at CRED submission in order to use them later?

So then I would query the wp-options table, trying to find an array where the address_passed = the address string stored in the Google Maps field, is that right?

Then I would store the lat and long for that particular array within the bigger array?

Does that all sound right?

#902474

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

They are not temporarily stored, they are permanently available from wp_options, unless you go to Toolset > Settings > Maps and delete the cache.

There is no reason to duplicate storing them in the database unless you have a particular reason to (e.g. you wanted to store them in wp_postmeta because you were going to create custom a WP_Query to retrieve posts based upon the coordinates, for example).

Again, I'm not sure where you are going to be using this, but the following would retrieve the lat and lon for the current post based upon its address field (where that field has a slug "address-field").

// get the address cache
$address_cache = get_option( 'toolset_maps_address_coordinates' );


// get the address field of the current post
$address = get_post_meta( get_the_ID(), 'wpcf-address-field', true );

// find the address within the cache
foreach ($address_cache as $key => $value) {
	if ( $value['address'] == $address ) {
		$lat = $value['lat'];
		$lon = $value['lon'];
		break;
	}
}

// we now have $lat and $lon for the current post

That is hopefully enough to get you started.

#902479

Maybe something like this?

add_action('cred_save_data_12', 'save_data_for_form_with_id_12',10,2);
function save_data_for_form_with_id_12($post_id, $form_data)
{
    if(isset($_POST['wpcf-location'])){
           $locationcache = getoption(toolset_maps_address_coordinates);
           foreach ($locationcache as $singlelocation) {
                  if ($singlelocation->address_passed = $_POST['wpcf-location']) {
                              update_post_meta($post_id, 'wpcf-long', $singlelocation->lon);
                              update_post_meta($post_id, 'wpcf-lat', $singlelocation->lat);
        }
      }
}
#902480

Oh sorry i didn't read your last reply before sending off my last message. I'll just go through that and see if it makes any sense.

Thanks again

#902482

Hi Nigel

Thats great. I think it all makes sense to me now.

Thanks for your help.