Skip Navigation

[Resolved] Automatically filling (longitude) and (latitude) fields using (address) field

This support ticket is created 2 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Shane 2 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#2455711
Screen Shot 2022-09-12 at 9.53.50 AM.png
Screen Shot 2022-09-12 at 9.52.17 AM.png

I have a (Post Field Group). Within that group, I have an (Address) field. When I edit a post, I see that post field group. For the address field, there is a (Show/Hide coordinates) link under the field. When I click on it, the (Latitude) and (Longitude) fields get displayed. How do I automatically get those fields to be stored in the database (DB) when I enter an address into the (Address) field?

When I look at my (*_postmeta) table, I can see all the fields from the (Post Field Group). But, the latitude and longitude information doesn't get stored.

#2455755

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kevin,

Thank you for getting in touch.

The longitude and Latitude value are being stored just not in the postmeta table. They are stored inside internal tables used for our plugins.

If you want to retrieve the longitude and latitude values you can use the types shortcodes like this,

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

Please let me know if this helps.
Thanks,
Shane

#2455811

Do I need to save latitude and longitude fields to my (Post Field Group) and populate those "manually?"

Or can I use those latitude and longitude fields that get displayed when the (Show/Hide coordinates) link is clicked?

I want to be able to store the address, longitude, and latitude data to the database.

#2455821

I have the following code to prefill some other location-related fields:

/* ----------------------------------------------------------------------------
KevinSikora: auto fill location related fields when a user enters an address
*/
add_action( 'admin_enqueue_scripts', 'custom_enqueue_admin_scripts' );
function custom_enqueue_admin_scripts(){
wp_enqueue_script( 'location-fields', get_theme_file_uri( '/assets/js/location-fields.js' ), array( 'jquery' ), '2.5', true );
}

That references the following code in (themes->Newspaper-child->assets->js->location-fields.js):

( function( $ ) {

$( document ).ready( function(){

$("input.js-toolset-google-map").on( "geocode:result", function( event, result ){
var geometry = result.geometry;

$('input[name="wpcf[coordinates-latitude]"]').val(geometry.location.lat);
$('input[name="wpcf[coordinates-longitude]"]').val(geometry.location.lng);

var addressComponents = result.address_components;

var locality = "";
var administrative_area_level_1 = "";
var countryShort = "";
var country = "";

var location = ""; // contains concatenated location to be displayed

$.each( addressComponents, function( index, value ){
if ( addressComponents[ index ].types[0] == "locality" ) {
locality = addressComponents[ index ].long_name;
}
else if ( addressComponents[ index ].types[0] == "administrative_area_level_1" ) {
administrative_area_level_1 = addressComponents[ index ].long_name;
}
else if ( addressComponents[ index ].types[0] == "country" ) {
countryShort = addressComponents[ index ].short_name;
country = addressComponents[ index ].long_name;
}
} );

$('input[name="wpcf[locality]"]').val(locality);
$('input[name="wpcf[administrative_area_level_1]"]').val(administrative_area_level_1);
$('input[name="wpcf[country]"]').val(country);

location = country;

if((countryShort === "US") && administrative_area_level_1) {
location = administrative_area_level_1;
} // need to handle Canada, Australia with city, territory, country

if(locality) {
location = locality + ", " + location;
}

$('input[name="wpcf[location]"]').val(location);
} );

} );

})( jQuery );

The above code works fine except for the following two lines:

$('input[name="wpcf[coordinates-latitude]"]').val(geometry.location.lat);
$('input[name="wpcf[coordinates-longitude]"]').val(geometry.location.lng);

When the result comes back, should it have the (geometry) information? Or do I need to do something special to get that information?

#2455861

If I can't automatically update a latitude and longitude field, how can I find these values in the plugin's internal tables? I'm thinking I could run a query to update the postmeta table with the values from the plugin's internal table.

Or if you have any other better ideas, I would really appreciate if you could let me know.

#2456467

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kevin,

Was checking to see if I could find the values being stored in the database. However it appears that this may have changed in a recent update as it was stored in the wp_options table based on my previous research.

The issue with your code from what I see if the input names are incorrect. The longitude and latitude fields are declared in HTML as the following.


<input id="toolset-google-map-late2e139f18a76" name="toolset-extended-form-wpcf[address][latitude]" class="js-toolset-google-map-latlon js-toolset-google-map-lat toolset-google-map-lat" type="text" value="">

<input id="toolset-google-map-lona6c08b0cccb2" name="toolset-extended-form-wpcf[address][longitude]" class="js-toolset-google-map-latlon js-toolset-google-map-lon toolset-google-map-lon" type="text" value="">

From there you should be able to use the correct information to assign the values.

Thanks,
Shane