首页 > 解决方案 > 谷歌地图自动完成限制不起作用

问题描述

我有以下内容:

  function initAutocomplete() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: 47.371068, lng: 8.538540},
          zoom: 500,
          zoomControl: false,
          disableDefaultUI: true
        });

        // Create the search box and link it to the UI element.
        var input = document.getElementById('pac-input');
        var searchBox = new google.maps.places.SearchBox(input);
        map.controls[google.maps.ControlPosition.TOP_CENTER].push(input);
        document.getElementById("bottomBar").style.opacity = "0.0";


        // Bias the SearchBox results towards current map's viewport.
        map.addListener('bounds_changed', function() {
          searchBox.setBounds(map.getBounds());
        });

        var markers = [];
        // Listen for the event fired when the user selects a prediction and retrieve
        // more details for that place.
        searchBox.addListener('places_changed', function() {
          var places = searchBox.getPlaces();

          if (places.length == 0) {
            return;
          }

这有效,但仅当我在搜索栏中键入时,它不仅限于 1 个国家/地区。

我尝试了以下方法:

// Create the search box and link it to the UI element.
        var input = document.getElementById('pac-input');
        var options = {
        componentRestrictions: {country: 'fr'}};
        var searchbox = new google.maps.places.Autocomplete(input,options);
        map.controls[google.maps.ControlPosition.TOP_CENTER].push(input);
        document.getElementById("bottomBar").style.opacity = "0.0";


        // Bias the SearchBox results towards current map's viewport.
        map.addListener('bounds_changed', function() {
          searchBox.setBounds(map.getBounds());
        });

        var markers = [];
        // Listen for the event fired when the user selects a prediction and retrieve
        // more details for that place.
        searchBox.addListener('places_changed', function() {
          var places = searchBox.getPlaces();

          if (places.length == 0) {
            return;
          }

搜索“有效”,因为我只能获得法国的位置,但是当我点击它们时,地图不会更新。

这里有什么问题?

完整版在这里

标签: javascriptgoogle-maps

解决方案


推荐阅读