谷歌地图自动完成功能无法在模态弹出窗口内工作

时间:2018-05-16 07:45:22

标签: javascript html angularjs google-maps

我正在使用谷歌地图api。我正在尝试为输入框实现自动完成地址功能。但问题是在页面中一切似乎都有效,但同样在模态弹出窗口内不起作用。

这是html

or $

这是模态弹出窗口

    <div class="col-xs-5 col-sm-2 col-md-2">
    <a href="javascript:void(0)" data-ng-click="addlocationpopup()">+New 
    Location</a>
    </div> <!--button which calls popup -->

控制器 JS

    <div class="modal right fade addlocation" id="myModal_5" tabindex="-1"
    role="dialog" aria-labelledby="myModalLabel2" data-keyboard="false">
    <div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h4 class="modal-title" id="myModalLabel1">ENTER LOCATION 
            DETAILS</h4>
        </div>
        <form name="locationForm" novalidate="novalidate" method="post">
            <div class="modal-body">
                <div class="row"> 
                  <div>
                  <input id="autocomplete" placeholder="Enter your address"
                  onFocus="geolocate()" type="text"></input>
                  </div>
                </div>
             </div>
         </form>
        </div>
       </div>
      </div>
    <script 
    src="https://maps.googleapis.com/maps/api/js?
    key=mykey&libraries=places&callback=initAutocomplete" async defer>
    </script>
    <script>
    var placeSearch, autocomplete;
    var componentForm = {
    street_number: 'short_name',
    route: 'long_name',
    locality: 'long_name',
    administrative_area_level_1: 'short_name',
    country: 'long_name',
    postal_code: 'short_name'
  };

  function initAutocomplete() {
   autocomplete = new google.maps.places.Autocomplete(
        /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
        {types: ['geocode']});
   autocomplete.addListener('place_changed', fillInAddress);
  }

  function fillInAddress() {
    var place = autocomplete.getPlace();

    for (var component in componentForm) {
      document.getElementById(component).value = '';
      document.getElementById(component).disabled = false;
    }

    for (var i = 0; i < place.address_components.length; i++) {
      var addressType = place.address_components[i].types[0];
      if (componentForm[addressType]) {
        var val = place.address_components[i][componentForm[addressType]];
        document.getElementById(addressType).value = val;
      }
    }
  }

  function geolocate() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var geolocation = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };
        var circle = new google.maps.Circle({
          center: geolocation,
          radius: position.coords.accuracy
        });
        autocomplete.setBounds(circle.getBounds());
      });
    }
  }
</script>

如果在页面i中放入一个输入框并输入它工作正常。但是在内部模态中它不起作用,当我在输入框内输入时,地方没有显示。我不明白为什么它不在弹出窗口内工作当页面被加载时,脚本已经被加载。任何人都可以告诉如何让它在模态弹出窗口中工作?

5 个答案:

答案 0 :(得分:4)

对于 Angular 4 +

::ng-deep .modal { z-index: 1001 !important;} 

::ng-deep .modal-backdrop {z-index: 1000 !important;}

::ng-deep .pac-container {z-index: 1055 !important;}

答案 1 :(得分:0)

最后我解决了这个问题。我使用z-index css功能来显示自动完成的地址,因为我在这样的输入框中输入内容

    <div class="modal right fade addlocation" id="myModal_5" tabindex="-1"
role="dialog" aria-labelledby="myModalLabel2" data-keyboard="false" style="z-index:2;">

现在我可以看到有关地址的建议。

答案 2 :(得分:0)

存在自动完成的建议结果,但隐藏在具有较高 z-index 值的弹出/模式窗口下方。要解决此问题,请在样式表中添加以下CSS:

div.pac-container {
    z-index: 99999999999 !important;
}

答案 3 :(得分:0)

我能够通过以下几行对其进行修复:

<style>
.modal { z-index: 1001 !important;} 
.modal-backdrop {z-index: 1000 !important;}
.pac-container {z-index: 1055 !important;}
</style>

答案 4 :(得分:0)

尝试对组件 scss 文件执行以下操作:

::ng-deep .pac-container {   
   z-index: 9999 !important;
}