Skip Navigation

[Resolved] Getting and using individual address components with “Address Field”

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

Problem:

Get the city/state information from custom address field.

Solution:

There isn't such kind of built-in feature within Toolset Maps plugin, it needs custom codes, for example:

https://toolset.com/forums/topic/getting-and-using-individual-address-components-with-address-field/#post-2069789

Relevant Documentation:

https://developers.google.com/maps/documentation/geocoding/overview

This support ticket is created 3 years, 5 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/Hong_Kong (GMT+08:00)

This topic contains 9 replies, has 2 voices.

Last updated by Don 3 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#2069717

Don

Hello, I just purchased Toolset (AMAZING!) and am watching the videos and starting to apply on my site... I am doing a test listing and checking out the address field. It works as expected, but a few things I definitely want to do when getting the address data via google api is:

1) in addition to the default "Address field" being saved as one complete location string I want to save individual api "address component" to separate Custom Fields. This way I have more flexibility on how to display the data in a template (for example on separate lines like AND to filter (like "by city" etc, maybe it can do that already).
2) While doing this, I also want to save an additional google api "address component" that is provided in the API for "Neighborhood" to a custom field.
3) Truncate/remove the "country" from address field during data collection and display since my site is ONLY for the US.

If this is all easily doable and I "missed it" in the tutorials or documentation i apologize for the support request but if isnt discussed by default but goals are doable i appreciate all tips! Looking forward to taking my site to a whole new level with Toolset!

Cheers, Don

#2069789

Dear Don,

Q1) Unfortunately, there isn't such kind of built-in feature within Toolset Maps plugin, in your case, it needs custom codes.
For example:
1) when you input value into a custom address field, and save the post, you can use WordPress built-in action hook save_post to trigger a custom PHP function:
https://developer.wordpress.org/reference/hooks/save_post/

2) Get the custom address field value:
https://toolset.com/documentation/customizing-sites-using-php/functions/
Pass it to Google Map API service:
hidden link
Get those values you wanted(City etc)

3) Then save them in different custom fields:
https://developer.wordpress.org/reference/functions/update_post_meta/

Q2) Toolset plugins support distance range filter, please check our document:
https://toolset.com/course-lesson/filtering-and-ordering-map-markers-by-distance/

Q3) As I mentioned in Q1) , you can get all parts of the full address value with Google Map API, then you can combine those specific parts into one value(without country), and save it to another custom field, then display this custom field in front-end

#2069797

Don

Thanks for the quick reply and tips! Just to clarify, for getting this address/location data i AM using YOUR toolset map plugin.... so isn't a "custom address field", ie different field, it is the default toolset map plugin address field. I plan to keep using it.... my hope is that since that field IS calling the google map api and getting the full address from the (I assume) "formatted_address" address component, I just want to know if easily possible to modify what the toolset map plugin does and make the other "address components" that are included available to be assigned to other toolset custom fields that i create, for each individual location field, like city, postal code, city, neighborhood..... since that data IS supplied in the same api call, i was hoping that after creating the appropriate custom fields a simple change to a toolset file would automaticallly parse that data to those fields like it already does for the "complete" address.... sorry if that makes it more confusing , but i wanted to be sure that my initial question was clear and not giving the impression i wanted to do something else....

#2069809

Yes, we are talking about the same thing, the "Address" field is one type of Toolset custom field.

And it is not recommended to hack the Toolset plugins files, it will be a problem when you upgrade plugins.

#2069857

Don

ok thank you. then perhaps i am missing something but it seems like filtering locations by, for example, all posts in a specific state, or city, or same zip code etc, instead of only by a distance range based on a specific address (lat and lng coordinates) is something that many people would want.... but if the individual location parts are not being saved i dont see how that is possible?

#2070673

I think there is a misunderstanding.

In order to filter the results by city(state ...), you need to save the city(state ...) values into different custom fields.
https://toolset.com/documentation/user-guides/views/filtering-views-by-custom-fields/

You can explode the full address value into parts with PHP codes, but you won't know which part is the city(state ...) value, please check my first answer above:
https://toolset.com/forums/topic/getting-and-using-individual-address-components-with-address-field/#post-2069789

The workflow is below:
1) Get the full address value from custom address field,
2) pass this value to Google Maps API, get city(state ...) values
3) save each value in different custom fields(Or you can setup the values manually)
4) so you will be able to sort the result by ity(state ...) fields

#2070859

Don

i really appreciate your time and patience on this... but i cant help feeling that either i am missing something "easy" about how the toolset maps addon works / can be used OR I am somehow really alone in

1) wanting to take advantage of the simple, elegant and address-validated address data collection that the map addon "address field" provides

BUT

2) ALSO want access / separate custom field data for any/all of the "address components" that the map addon DOES have access to when using the google api to get the "full address" and lat/lng values, which are apparently put into the toolset_maps_address_cache table, so that I can display the "address" info more flexibly, like on separate line which is typical:

Street address
City, State (or abbreviation) postal code
(Country)

AND would also be able to use them as filters (ie filter by state, city or zip)

Do I REALLY have to make a choice between using the toolset map addon, but being limited to a SINGLE address field for location data OR not using it and using individual custom fields for the each address part that a user has to enter.... but run the risk of it not being "valid" on top of the extra work for users compared to just selecting an autcompleted location suggestion.

I guess I am just surprised that either nobody else wants to have BOTH the simplicity offered by the map addon AND still have access to individual location data like city, zip etc....

I hope that there IS a solution to this, i am happy to create whatever custom fields to map to, since the map addon doesnt create individual location data columns in a table by default, and even willing to modify the code (if i knew in which specific php file the location data is collected and assigned) but still hope that i am just overlooking an easier, built in solution.

#2070913

As I mentioned above, there isn't such kind of built-in feature within Toolset plugins, it needs custom codes, if you need more assistance for it, please provide a test site with the same problem, fill below private message box with login details + FTP access, I can setup a demo for you

#2071035

Don

hello, then i must say i am really confused because this functionality DOES exist on your own demo site here:

hidden link

There you can clearly see that filtering by STATES that have listings as well as CITIES within a selected state is possible.

To make sure that it wasnt because a poster was literally adding those individual fields i REGISTERED on the site and started the listing process.... only to find the exact same single autocomplete address field!

So, clearly there is a misunderstanding here or I am missing something... or there is something else going one here because the demo site is doing EXACTLY what i wanted to achieve!

#2071897

Don

Hello, I am going with a different solution but I REALLY think that while using the autocomplete field /map addon it SHOULD be available to admins to define what data, if any besides a single line address, can and should be mapped to admin specified fields (wordpress default fields or toolset custom fields or taxonomies). All that location data is provided via the Google api and can be very important for people where location data is one of the most important things to display and filter by.... thanks for the consideration!