Skip to content Skip to sidebar Skip to footer

Retrieve Info From Google Maps Autocomplete

I've this code that I use to generate a google map with autocomplete e some field that will autofill with info like lat, lng, adress and town. The problem is that I cant archieve t

Solution 1:

I get a javascript error with your code: Uncaught ReferenceError: results is not defined on this line:

var town = extractFromAdress(results[0].address_components, "locality");

That should be the place object you retrieved (that you got the rest of the information from):

var place = autocomplete.getPlace();

Then it works (if the result has a result of type "locality")

var town = extractFromAdress(place.address_components, "locality");
document.getElementById('city').value = town;

proof of concept fiddle

code snippet:

functioninitMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: -33.8688,
      lng: 151.2195
    },
    zoom: 13
  });
  var options = {
    componentRestrictions: {
      country: "it"
    }
  };

  var input = /** @type {!HTMLInputElement} */ (
    document.getElementById('pac-input'));

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

  var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
    map: map,
    anchorPoint: new google.maps.Point(0, -29),
  });

  autocomplete.addListener('place_changed', function() {
    infowindow.close();
    marker.setVisible(false);
    var place = autocomplete.getPlace();

    if (!place.geometry) {
      window.alert("Autocomplete's returned place contains no geometry");
      return;
    }

    // If the place has a geometry, then present it on a map.if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17); // Why 17? Because it looks good.
    }
    marker.setIcon( /** @type {google.maps.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(35, 35)
    }));
    marker.setPosition(place.geometry.location);
    marker.setVisible(true);

    var address = '';
    if (place.address_components) {
      address = [
        (place.address_components[0] && place.address_components[0].short_name || ''), (place.address_components[1] && place.address_components[1].short_name || ''), (place.address_components[2] && place.address_components[2].short_name || '')
      ].join(' ');
    }

    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
    infowindow.open(map, marker);

    var adress_form = address;
    document.getElementById('adress').value = adress_form

    var lat_form = place.geometry.location.lat();
    document.getElementById('lat').value = lat_form

    var lng_form = place.geometry.location.lng();
    document.getElementById('lng').value = lng_form

    functionextractFromAdress(components, type) {
      for (var i = 0; i < components.length; i++)
        for (var j = 0; j < components[i].types.length; j++)
          if (components[i].types[j] == type) return components[i].long_name;
      return"";
    }

    var town = extractFromAdress(place.address_components, "locality");
    document.getElementById('city').value = town;
  });
}
google.maps.event.addDomListener(window, 'load', initMap);
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#map {
  height: 100%;
}
.controls {
  margin-top: 10px;
  border: 1px solid transparent;
  border-radius: 2px002px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  height: 32px;
  outline: none;
  box-shadow: 02px6pxrgba(0, 0, 0, 0.3);
}
#pac-input {
  background-color: #fff;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  margin-left: 12px;
  padding: 011px013px;
  text-overflow: ellipsis;
  width: 300px;
}
#pac-input:focus {
  border-color: #4d90fe;
}
.pac-container {
  font-family: Roboto;
}
#type-selector {
  color: #fff;
  background-color: #4d90fe;
  padding: 5px11px0px11px;
}
#type-selectorlabel {
  font-family: Roboto;
  font-size: 13px;
  font-weight: 300;
}
<scriptsrc="https://maps.googleapis.com/maps/api/js?libraries=places"></script><inputid="pac-input"class="controls"type="text"placeholder="Enter a location"><divid="map"style="width:500px; height:500px;"></div><formmethod="post"action="a.php"><inputplaceholder="adress"type="text"id="adress"name="adress"><inputplaceholder="city"type="text"id="city"name="city"><inputplaceholder="lat"type="text"id="lat"name="lat"><inputplaceholder="lng"type="text"id="lng"name="lng"><inputplaceholder="latlng"type="text"id="latlng"name="latlng"><inputplaceholder="name"type="text"id="name"name="name"><inputtype="submit"value="inserisci"></form>

Post a Comment for "Retrieve Info From Google Maps Autocomplete"