質問

First time poster here. Having some trouble getting the country restriction working on a search field. My code is currently:

<script type="text/javascript"
    src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false">
</script>
<script type="text/javascript">
function initialize() {
    var mapOptions = {
        //center: new google.maps.LatLng(-33.8688, 151.2195),
        zoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById('map_canvas'),
    mapOptions);

    var input = document.getElementById('searchTextField');
    var autocomplete = new google.maps.places.Autocomplete(input);

    autocomplete.bindTo('bounds', map);
    // Autocomplete.
    function setupClickListener(id, types) {
        var radioButton = document.getElementById(id);
        google.maps.event.addDomListener(radioButton, 'click', function() {
            autocomplete.setTypes(types);               
        });
    }    
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>

What would I need to to do to get this to limit by country? I've tried adding:

   componentRestrictions: {country: 'us'}

And this stops the autocomplete working altogether, no obvious syntax errors.

Forgive me if it seems lazy but I'm relatively new to programming so need all the help I can get! I'm trying to modify an existing instance of the API here so didn't code it to start with which is undoubtedly adding to my confusion.

Thank you.

役に立ちましたか?

解決

Use equal sign instead of colon:

var componentRestrictions = {country: 'us'};
autocomplete.setComponentRestrictions(componentRestrictions);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top