furious-luke / django-address

A Django address model and field. Addresses may be specified by address components or by performing an automatic Google Maps lookup.
BSD 3-Clause "New" or "Revised" License
431 stars 181 forks source link

Restrict Autocomplete to Specific Countries #113

Closed masaruduy closed 4 years ago

masaruduy commented 4 years ago

Thanks for you great app you made.

Is there a way to restrict the autocomplete results to certain countries? I've found this setting on https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-multiple-countries: autocomplete.setComponentRestrictions( {'country': ['us', 'pr', 'vi', 'gu', 'mp']});

Not sure how it could be used together with your code though. Can you please advise?

Thanks!

banagale commented 4 years ago

@masaruduy

Currently there is no way to pass this option in without modifying the package. However, I was able to achieve this by modifying the address.js file.

Here's the line in question.

Add the countries you want to a country property like this:

$(function () {
    $('input.address').each(function () {
        var self = $(this);
        var cmps = $('#' + self.attr('name') + '_components');
        var fmtd = $('input[name="' + self.attr('name') + '_formatted"]');
        self.geocomplete({
            **country: ['IT', 'US'],**
            details: cmps,
            detailsAttribute: 'data-geo'        
...

And you get only those countries:

Screen Shot 2020-06-17 at 12 39 26 PM

I'm closing this for now, but if it comes up again we can look at adding it as a feature.