Places search box

by Vo Tai Tri

HTML

<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places&language=vi&region=VI&callback=initMap"
         async defer></script>

CSS

/* Always set the map height explicitly to define the size of the div
 * element that contains the map. */
#map {
  height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
.controls {
  margin-top: 10px;
  border: 1px solid transparent;
  border-radius: 2px 0 0 2px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  height: 32px;
  outline: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

#pac-input {
  background-color: #fff;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  margin-left: 12px;
  padding: 0 11px 0 13px;
  text-overflow: ellipsis;
  width: 300px;
}

#pac-input:focus {
  border-color: #4d90fe;
}

.pac-container {
  font-family: Roboto;
}

#type-selector {
  color: #fff;
  background-color: #4d90fe;
  padding: 5px 11px 0px 11px;
}

#type-selector label {
  font-family: Roboto;
  font-size: 13px;
  font-weight: 300;
}
#target {
  width: 345px;
}

JavaScript

var map, marker, infowindow;
var markers = [];
var address_infos = {};

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 15,
    center: { lat: 21.0167904, lng: 105.7819856 },
    streetViewControl: false,
    mapTypeControl: false
  });
  
  placeMarker({ lat: 21.0167904, lng: 105.7819856 });
  geocodeAddress(marker.position);
  map.panTo(marker.position);
  markers.push(marker);

  map.addListener("click", function(e) {
    clearMarkers();
    placeMarker(e.latLng);
    geocodeAddress(e.latLng);
    map.panTo(marker.position);
    markers.push(marker);
  });

  createInfoWindow();
  
  // Create the search box and link it to the UI element.
  var input = document.getElementById('pac-input');
  var searchBox = new google.maps.places.SearchBox(input);
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

  // Bias the SearchBox results towards current map's viewport.
  // map.addListener('bounds_changed', function() {
    // searchBox.setBounds(map.getBounds());
  // });

  searchBox.addListener("places_changed", function() {
    searchBox.set("map", null);
    clearMarkers();

    var places = searchBox.getPlaces();
    if (places.length == 0) {
      console.log("Returned no place");
      return;
    }

     var bounds = new google.maps.LatLngBounds();

     if (places.length > 1) {
      places.forEach(function(place) {
        if (!place.geometry) {
          console.log("Returned place contains no geometry");
          return;
        }

        var icon = {
          url: place.icon,
          size: new google.maps.Size(71, 71),
          origin: new google.maps.Point(0, 0),
          anchor: new google.maps.Point(17, 34),
          scaledSize: new google.maps.Size(25, 25)
        };

        marker = new google.maps.Marker({
          map: map,
          icon: icon,
          title: place.name,
          position: place.geometry.location
        });

        marker.bindTo("map", searchBox, "map");
 ...