Skip Navigation

[Resolved] maps autocomplete restrict to 1 country, cities and postcode

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

Problem:

I want to limit the auto suggestion from de distance center field to just Netherlands.

Solution:

It needs custom JS coeds, for example:

https://toolset.com/forums/topic/maps-autocomplete-restrict-to-1-country-cities-and-postcode/#post-2186485

Relevant Documentation:

This support ticket is created 3 years, 1 month 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 6 replies, has 2 voices.

Last updated by herbie4 3 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#2182995

Tell us what you are trying to do?

I want to limt the auto suggest from de distance center field to just Netherlands.
And suggestions should only look at postcode or city name.

Your colleague Minesh implemented this, but it seems not to be working correct.
When I type 'zoe' I can see suggestions from other countries then NL.

Is there any documentation that you are following?
Here is the current code from loop editor JS tab:

function initialize() {
var input = document.getElementsByClassName('js-toolset-maps-distance-center');
options = {
language: 'nl-NL',
types: [],
componentRestrictions: { country: 'nl' }
};

var autocomplete = new google.maps.places.Autocomplete(input[0],options);
}

jQuery(document).ready(function($){
google.maps.event.addDomListener(window, 'load', initialize);
});

Hope you can help.

#2183367

Hello,

I assume we are talking about the distance filter in custom search form, if it is, please try the custom JS workaround in below Knowledge Base link:
hidden link

#2183445

Hi Luo,
I added this in the loop editor js tab instead of the other script but it still does not work. Any ideas?

( function( $ ) {
	$( document ).bind( 'js_event_wpv_addon_maps_init_map_completed', function(){

        var input = document.getElementById('toolset-maps-distance-center');
        var options = {
        types: ['(cities)'],
        componentRestrictions: {country: 'nl'}
        };

        autocomplete = new google.maps.places.Autocomplete(input, options);

	});
})( jQuery );
#2184313

Did you get any JS errors?
Since it is a custom codes problem, please provide a test site with the same problem, fill below private message box with login details, also point out the problem page URL and view URL, I need to test and debug it in a live website, thanks

#2186485
country4.JPG

Since those JS codes need to take effect on search form, I have moved the custom JS codes into section "Search and Pagination", input box "JS editor", and modified them as below:

( function( $ ) {
	$( document ).on( 'click', '#toolset-maps-distance-center', function( event ) {

      	event.preventDefault();
        var input = document.getElementById('toolset-maps-distance-center');
        var options = {
        //types: ['(cities)'],
        componentRestrictions: {country: 'NL'}
        };

        autocomplete = new google.maps.places.Autocomplete(input, options);

	});
})( jQuery );

Test it in frontend, it works fine, see my screenshot nl.JPG

Please test again, check if it is what you want.

#2187225

Hi Luo,

Thank you for the code. That worked. Only I wanted to show just pc and city auto suggestions, so I changed your code a little to:

types: ['(regions)'],
        componentRestrictions: {'country': "NL"}

Now it is working great.
Thank you!

#2187227

My issue is resolved now. Thank you!