How to get the County from an address provided

By: sam thornal | Asked: 08/12/2023
ForumsCategory: General questionsHow to get the County from an address provided
sam thornalsam thornal asked 1 year ago
I am using the address field with the "Add address autocomplete" checked which is great. However has anyone figured out a clever way to get the zip code for this address. In Texas we can have zip codes/cities that can span over two or more counties. I am assuming I would need to use the lat/long that is captured when using the address auto complete, but not sure how I could us it to query another site like google? first time here in the community so sorry in advance if I mess up on proper etiquette,
1 Answers
Victor Font answered 1 year ago
What do you mean "get the zip code for this address?" Address autocomplete already uses the Google Geocoding API, Maps JavaScript API, and Places API. Are you saying the zip code is not being populated by Google?
sam thornalsam thornal replied 1 year ago

Sorry I said zip code but meant County like I had in the initial question. How do I get the County for an address provided by a client.

Victor Font replied 1 year ago

I never use the address field. So, I don't know. I always integrate Google places myself so I have full access to the Google map object.

sam thornalsam thornal replied 1 year ago

How do you integrate with Google Places, does it have the County for the address?

Victor Font replied 1 year ago

I was integrating Google's API long before there was a Geolocation add-on. The Places API has a lot of information in it, including the following:

{
"long_name": "Australia",
"short_name": "AU",
"types": ["country", "political"],
},

The shortcoming with the Geolocation add-on is that the data object returned from Google's API is scoped inside of a non-static function in a final OOP class. Final classes cannot be extended. This renders the API object inaccessible to further manipulation by a developer.

The Strategy 11 developers can resolve this by adding a filter to the function, but I suppose someone would have to open a ticket and ask for a filter as a feature request.

In the meantime, to access all of Google's APIs features, it's a DIY process. You can use JavaScript or the webservice to get the full data object. See this: https://developers.google.com/maps/documentation/places/web-service/details

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right