<!DOCTYPE html>
<html>

  <head>
    <link data-require="bootstrap@4.1.1" data-semver="4.1.1" rel="stylesheet" 
    href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="style.css" />
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=places&API=AIzaSyBKyhXsbW_uw5eq-G5jzZS4SMW7TICTh_M"></script>
    <script type="text/javascript" src="script.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <script type="text/javascript">
      window.onload = function() {  
        initialize();
      }
    </script>
  </head>

  <body>
    <div class="container container-fluid">
    <div class="row">
      <div class="col-md-12">
        <h1>Find a place...</h1>
        <div id="locationField">
          <input class="form-control" id="autocomplete" placeholder="Enter your address" onfocus="geolocate()" type="text" />
        </div> 
      </div>   
    </div>
    </div>
  </body>

</html>
var placeSearch, autocomplete, autocomplete_postalcode, autocomplete_city;
var componentForm = {
  street_number: 'short_name',
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  country: 'long_name',
  postal_code: 'short_name'
};
var map;

function initialize() {

  // Create the autocomplete object, restricting the search
  // to geographical location types.
  autocomplete = new google.maps.places.Autocomplete(
    /** @type {HTMLInputElement} */
    (document.getElementById('autocomplete')), {
      types: ['geocode']
    });
  // When the user selects an address from the dropdown,
  // populate the address fields in the form.
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    fillInAddress();
  });
  
  autocomplete_postalcode = new google.maps.places.Autocomplete(
    (document.getElementById("AutocompletePostalCode")), {
     types: ['(regions)'],
     componentRestrictions: {country: 'fr'}
    });
  google.maps.event.addListener(autocomplete_postalcode, 'place_changed', function(){
    var place = autocomplete_postalcode.getPlace();
    console.log('place: ' + place);
  });
  
  autocomplete_city = new google.maps.places.Autocomplete(
    (document.getElementById('AutocompleteCity')), {
      types: ['(cities)'],
      componentRestrictions: {country: 'fr'}
    });
  google.maps.event.addListener(autocomplete_city, 'place_changed', function(){
    var place = autocomplete_city.getPlace();
    console.log('place from city search: ' + place);
  });
   
  var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(-34.397, 150.644)
  };
  map = new google.maps.Map(document.getElementById('map'), mapOptions);
}

function fillInAddress() {
  // Get the place details from the autocomplete object.
  var place = autocomplete.getPlace();

  for (var component in componentForm) {
    document.getElementById(component).value = '';
    document.getElementById(component).disabled = false;
  }

  // Get each component of the address from the place details
  // and fill the corresponding field on the form.
  for (var i = 0; i < place.address_components.length; i++) {
    var addressType = place.address_components[i].types[0];
    if (componentForm[addressType]) {
      var val = place.address_components[i][componentForm[addressType]];
      document.getElementById(addressType).value = val;
    }
  }
}

// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = new google.maps.LatLng(
        position.coords.latitude, position.coords.longitude);
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}
/* Styles go here */

.pac-container {
  position: absolute;
  top: 100%;
  left: 0;
  float: left;
  min-width: 10rem;
  padding: 0.5rem 0;
  margin: 0.125rem 0 0;
  list-style: none;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 0.25rem;
}

.pac-item {
  font-size: 14px;
  font-family: Helvetica;
  width: 100%;
  color: #3d3d3d;
  height: 50px;
  line-height: 50px;
  vertical-align: middle;
  white-space: nowrap;
  background: none;
  border: 0;
}

.pac-item:hover,
.pac-item:focus {
  color: #fff;
  text-decoration: none;
  background-color: #2574A9;
}

.pac-item-selected {
  text-decoration: none;
  background-color: #f0f0f0;
}

.pac-icon {
  display:none;
  /*
  vertical-align: middle;
  background-repeat: no-repeat;
  background-position: 0px 0px;
  background-size: 17px 18px;
  background-image:url("http://www.free-icons-download.net/images/map-marker-icons-95201.png");
*/
}

.pac-matched {}

.pac-item-query {
  font-size: 18px;
  color: #2e2e2e;
  font-weight: bold;
  margin-left: 1em;
}
/* read this before use
https://developers.google.com/maps/terms#9-license-requirements#section_9_4
.pac-container:after {
  background-image: none;
  height: 0;
}
*/