JSFiddle - React, Tailwind, and code Playground
by ofirattia
HTML
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&ext=.js"></script>
<div id="map"></div>
<div class="nav"><input id="pac-input" class="controls" type="text" placeholder="e.g. Aurora Provisions"></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...
CSS
#map {
height: 600px;
width: 100%;
z-index: 1;
opacity:0.6;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
.nav {
position: absolute;
top: 1px;
left:45%;
width: 300px;
height: 48px;
background-color: blue;
z-index: 2;
opacity:0.6
}
.controls {
position: absolute;
float: right;
background-color: #fff;
border-radius: 2px;
border: 1px solid transparent;
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: 200px;
z-index: 10;
}
.controls:focus {
border-color: #4d90fe;
z-index: 10;
}
.title {
font-weight: bold;
}
#infowindow-content {
display: none;
}
#map #infowindow-content {
display: inline;
}