2017-08-26 37 views
1

我很難過。我需要將搜索欄放在地圖頂部的半透明div中。我已經嘗試了嵌套,而不是嵌套,z-index的每一個組合,並且無論搜索欄位於div下面,但是我不能單擊它,並且它僅可見,因爲div是透明的...如何讓谷歌地圖搜索欄坐在一個半透明的全寬div

The問題是與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>

回答

0

您需要刪除此行:

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

這是刪除自動完成輸入元素並將其添加到地圖(所以它是在「導航」div)。

然後您需要調整其放置位置(或放置地圖類型控件)以消除重疊。

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>

+0

這是一個很大的幫助 - 我仍然在努力讓搜索欄和覆蓋坐在地圖上,但這絕對是問題所在。我也沒有完全理解信息如何在沒有這條線的情況下被推動,但是我會花一些時間來理解它的黑魔法。非常感謝您花時間幫助我。 – CorradoRossi