Bootstrap模式对话框中的Google地图自动填充结果

Fur*_*moe 66 html javascript css google-maps-api-3 twitter-bootstrap

我在Twitter Bootstrap模式对话框中有一个Google Maps Autocomplete输入字段,并且不显示自动完成结果.但是,如果我按向下箭头键,它会选择下一个自动完成结果,因此似乎自动完成代码正常工作,只是结果未正确显示.也许它隐藏在模态对话框后面?

这是截图:

在自动填充字段中键入内容不会提供任何内容 在自动填充字段中键入内容不会提供任何内容

按向下箭头键可得到第一个结果 按向下箭头键可得到第一个结果

和代码:

<div class="modal hide fade" id="map_modal">
<div class="modal-body">
    <button type="button" class="close" data-dismiss="modal">×</button>
    <div class="control-group">
        <label class="control-label" for="keyword">Cari alamat :</label>
        <div class="controls">
            <input type="text" class="span6" name="keyword" id="keyword">
        </div>
    </div>
    <div id="map_canvas" style="width:530px; height:300px"></div>
</div>
<div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
    <a href="#" class="btn btn-primary">Save changes</a>
</div>
Run Code Online (Sandbox Code Playgroud)

<script type="text/javascript">
$("#map_modal").modal({
    show: false
}).on("shown", function()
{
    var map_options = {
        center: new google.maps.LatLng(-6.21, 106.84),
        zoom: 11,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"), map_options);

    var defaultBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(-6, 106.6),
        new google.maps.LatLng(-6.3, 107)
    );

    var input = document.getElementById("keyword");
    var autocomplete = new google.maps.places.Autocomplete(input);
    autocomplete.bindTo("bounds", map);

    var marker = new google.maps.Marker({map: map});

    google.maps.event.addListener(autocomplete, "place_changed", function()
    {
        var place = autocomplete.getPlace();

        if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
        } else {
            map.setCenter(place.geometry.location);
            map.setZoom(15);
        }

        marker.setPosition(place.geometry.location);
    });

    google.maps.event.addListener(map, "click", function(event)
    {
        marker.setPosition(event.latLng);
    });
});
</script>
Run Code Online (Sandbox Code Playgroud)

我已尽力自己解决这个问题,但由于我不知道自动完成结果的html&css(检查元素什么都没有),我现在迷路了.有帮助吗?

谢谢!

Lui*_*uis 108

问题是用z-index.modal

使用此CSS标记:

.pac-container {
    background-color: #FFF;
    z-index: 20;
    position: fixed;
    display: inline-block;
    float: left;
}
.modal{
    z-index: 20;   
}
.modal-backdrop{
    z-index: 10;        
}?
Run Code Online (Sandbox Code Playgroud)

您也可以在这里查看最终演示

ps:感谢@lilina在jsfiddle.com上的演示

  • 我发现这把我的模态放在我的导航栏后面,看起来不对.尝试将.pac-container的z索引更改为1050 (8认同)
  • .pac-container {z-index:1070; 只是你需要的东西.如果您的页面中有.navbars,则它们具有不同的z-Index,而不是更改所有引导程序z-index,也可以在GMaps上执行z-index.GeoComplete.js也会出现此问题 (3认同)

小智 54

.pac-container {
    z-index: 1051 !important;
}
Run Code Online (Sandbox Code Playgroud)

为我做了

https://github.com/twbs/bootstrap/issues/4160


Rud*_*aya 14

Bootstrap .modal z-index默认为1050.

jQuery UI .ui-autocomplete z-index默认为3.

所以把它放在CSS上:

/* Fix Bootstrap .modal compatibility with jQuery UI Autocomplete,
see http://stackoverflow.com/questions/10957781/google-maps-autocomplete-result-in-bootstrap-modal-dialog */
.ui-autocomplete {
    z-index: 1051 !important;
}
Run Code Online (Sandbox Code Playgroud)

作品奇迹!:)


Ped*_*ado 10

使用Bootstrap 3:

.pac-container {
  z-index: 1040 !important;
}
Run Code Online (Sandbox Code Playgroud)

  • 我需要将其更改为1050然后才有效 (4认同)

vin*_*lcy 6

在我的方案中,即使我在CSS中设置,.pac-container的z-index值也将被初始化并覆盖为1000。(可能是通过Google API?)

我的肮脏修复

$('#myModal').on('shown', function() {
     $(".pac-container").css("z-index", $("#myModal").css("z-index"));
}
Run Code Online (Sandbox Code Playgroud)


小智 6

我花了 2.3 个小时来找出 google place API drop down z index 的错误。最后我找到了这个。只需将此代码粘贴到 JS 脚本顶部并更新输入 ID 名称即可。

var pacContainerInitialized = false;
$("#inputField").keypress(function() {
  if (!pacContainerInitialized) {
    $(".pac-container").css("z-index", "9999");
    pacContainerInitialized = true;
  }
});
Run Code Online (Sandbox Code Playgroud)

完整参考链接。 https://groups.google.com/forum/#!topic/google-maps-js-api-v3/GZX0ynQNn1M 感谢 Gautam。