javascript - 如何让 Google map 搜索栏位于半透明全宽 div 中

标签 javascript css api google-maps maps

我被难住了。我需要将搜索栏放在 map 顶部的半透明 div 中。我已经尝试了嵌套、非嵌套、z-index 的每种组合,无论搜索栏位于 div 下方,但我无法单击它,它只是可见,因为 div 是透明的...

问题出在我称为 class="nav"的 div

function initMap() {
   var map = new google.maps.Map(document.getElementById('map'), {
     center: {lat: 43.6615, lng: -70.2553},
     zoom: 15
   });

   var input = document.getElementById('pac-input');

   var autocomplete = new google.maps.places.Autocomplete(input);
   autocomplete.bindTo('bounds', map);

   map.controls[google.maps.ControlPosition.TOP_RIGHT].push(input);

   var infowindow = new google.maps.InfoWindow();
   var infowindowContent = document.getElementById('infowindow-content');
   infowindow.setContent(infowindowContent);
   var marker = new google.maps.Marker({
     map: map
   });
   marker.addListener('click', function() {
     infowindow.open(map, marker);
   });

   autocomplete.addListener('place_changed', function() {
     infowindow.close();
     var place = autocomplete.getPlace();
     if (!place.geometry) {
       return;
     }

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

     // Set the position of the marker using the place ID and location.
     marker.setPlace({
       placeId: place.place_id,
       location: place.geometry.location
     });
     marker.setVisible(true);

     infowindowContent.children['place-name'].textContent = place.name;
     infowindowContent.children['place-id'].textContent = place.place_id;
     infowindowContent.children['place-address'].textContent = place.formatted_address;
     infowindow.open(map, marker);
   });
 }
#map {
  height: 600px;
  width: 100%;
  z-index: 1;
}

/* Optional: Makes the sample page fill the window. */
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.nav {
  position: absolute;
  top: 1px;
  width: 100%;
  height: 48px;
  background-color: blue;
  opacity: .6;
  z-index: 2;
}

.controls {
  position: absolute;
  float: right;
  background-color: #fff;
  border-radius: 2px;
  border: 1px solid transparent;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  box-sizing: border-box;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  height: 29px;
  margin-left: 17px;
  margin-right: 17px;
  margin-top: 10px;
  outline: none;
  padding: 0 11px 0 13px;
  text-overflow: ellipsis;
  width: 400px;
  z-index: 10;
}

.controls:focus {
  border-color: #4d90fe;
  z-index: 10;
}
.title {
  font-weight: bold;
}
#infowindow-content {
  display: none;
}
#map #infowindow-content {
  display: inline;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" width="device-width, initial-scale=1">
    <title>iBec Map Project</title>
    <link href="style.css" rel="stylesheet">
  </head>
  <body>

<input id="pac-input" class="controls" type="text" placeholder="e.g. Aurora Provisions">
  <div id="map"></div>
  <div class="nav"></div>
  <div id="infowindow-content">
    <span id="place-name"  class="title"></span><br>
       Place ID <span id="place-id"></span><br>
    <span id="place-address"></span>
  </div>

    <script src="project.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDu3PmYIdOr0EebzqQL2sGQCjrKaUzkONg&libraries=places&callback=initMap"
    async defer></script>
  </body>
</html>

最佳答案

您需要删除此行:

map.controls[google.maps.ControlPosition.TOP_RIGHT].push(input);

这就是删除自动完成输入元素并将其添加到 map 中(因此它位于“nav”div 下)。

然后,您需要调整其位置(或 map 类型控件的位置)以消除重叠。

proof of concept fiddle

代码片段:

#map {
  height: 600px;
  width: 100%;
  z-index: 1;
}


/* Optional: Makes the sample page fill the window. */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.nav {
  position: absolute;
  top: 1px;
  width: 100%;
  height: 48px;
  background-color: blue;
  opacity: .6;
  z-index: 2;
}

.controls {
  position: absolute;
  float: right;
  background-color: #fff;
  border-radius: 2px;
  border: 1px solid transparent;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  box-sizing: border-box;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  height: 29px;
  margin-left: 17px;
  margin-right: 17px;
  margin-top: 10px;
  outline: none;
  padding: 0 11px 0 13px;
  text-overflow: ellipsis;
  width: 400px;
  z-index: 10;
}

.controls:focus {
  border-color: #4d90fe;
  z-index: 10;
}

.title {
  font-weight: bold;
}

#infowindow-content {
  display: none;
}

#map #infowindow-content {
  display: inline;
}
<title>iBec Map Project</title>

<input id="pac-input" class="controls" type="text" placeholder="e.g. Aurora Provisions">
<div id="map"></div>
<div class="nav"></div>
<div id="infowindow-content">
  <span id="place-name" class="title"></span><br> Place ID <span id="place-id"></span><br>
  <span id="place-address"></span>
</div>
<script>
  function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: {
        lat: 43.6615,
        lng: -70.2553
      },
      zoom: 15
    });

    var input = document.getElementById('pac-input');

    var autocomplete = new google.maps.places.Autocomplete(input);
    autocomplete.bindTo('bounds', map);

    //   map.controls[google.maps.ControlPosition.TOP_RIGHT].push(input);

    var infowindow = new google.maps.InfoWindow();
    var infowindowContent = document.getElementById('infowindow-content');
    infowindow.setContent(infowindowContent);
    var marker = new google.maps.Marker({
      map: map
    });
    marker.addListener('click', function() {
      infowindow.open(map, marker);
    });

    autocomplete.addListener('place_changed', function() {
      infowindow.close();
      var place = autocomplete.getPlace();
      if (!place.geometry) {
        return;
      }

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

      // Set the position of the marker using the place ID and location.
      marker.setPlace({
        placeId: place.place_id,
        location: place.geometry.location
      });
      marker.setVisible(true);

      infowindowContent.children['place-name'].textContent = place.name;
      infowindowContent.children['place-id'].textContent = place.place_id;
      infowindowContent.children['place-address'].textContent = place.formatted_address;
      infowindow.open(map, marker);
    });
  }
</script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initMap" async defer></script>

关于javascript - 如何让 Google map 搜索栏位于半透明全宽 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45897809/

相关文章:

c# - 是否有 C# API 或 Web 服务来获取 NSE 或 BSE 的报价?

javascript - Angular 模块加载失败

javascript - 将变量从 while 循环内传递到 popupdiv

javascript - jp@gc 的脚本 - WebDriver Sampler

javascript - react Material -UI :How to disable tooltip after click it

api - 使用对外部服务器的 HTTP 调用来检查安全性的安全后果是什么?

api - Office365 日历 REST Api 中日历 View 端点每页返回的最大事件数是多少?

javascript - Firebase 使用电子邮件和密码创建用户并将数据放入数据库

html - 如何使用 "div"和 "css"重组页面?

css - 未触发 HTML5 放置事件。