Google GeoCode Autocomplete limit State

后端 未结 2 1795
南旧
南旧 2021-01-20 01:26

I am trying to add an Google Geocode Autocomplete field to my form.

I have been able to get it to restrict the results returned to the country (AU) but it would be e

相关标签:
2条回答
  • 2021-01-20 01:51

    Keep a list of state lat/long boundaries in your app and restrict autocomplete with them.

        var stateBounds={
            ca: ["32.812736", "-119.216815", "34.608128", "-117.039301"]
            // lat/long boundaries list for states/provinces.
        };
    
        function getStateBounds(state) {
            return new google.maps.LatLngBounds(
              new google.maps.LatLng(stateBounds[state][0], 
                                     stateBounds[state][1]), 
              new google.maps.LatLng(stateBounds[state][2], 
                                     stateBounds[state][3])
            ); 
        }
    
        new google.maps.places.Autocomplete(document.getElementById('search'), {
            types: ['address'],
            componentRestrictions: {country: 'us'},
            bounds: getStateBounds('ca') // get LatLngBounds for ca.
        });
    
    0 讨论(0)
  • 2021-01-20 01:57

    I use autocomplete without any sort of filter on it but automatically append the state I'm interested in to all of the user input before sending the query.

    For someone who wants California results, for example, if they were to search for Sacramento it would automatically correct the input to Sacramento, California and this has worked somewhat well for me. You could be even more specific with the data you send, for example Sacramento, California, USA

    0 讨论(0)
提交回复
热议问题