squillace91
12/17/2017 - 11:51 PM

Google Maps API Autocomplete

Code to get lat and lng using google maps autocomplete API


    <head>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
    </head>
    <body>
        <label for="locationTextField">Location</label>
        <input id="locationTextField" type="text" size="50">
         
        <script>
            function init() {
        var autocomplete = new google.maps.places.Autocomplete(document.getElementById("locationTextField"));

        google.maps.event.addListener(autocomplete, 'place_changed', function () {

        var place = autocomplete.getPlace();
        var location = "Address: " + place.formatted_address + "<br/>";
   location += "Latitude: " + place.geometry.location.lat() + "<br/>";
      location += "Longitude: " + place.geometry.location.lng();
      console.log(location)
        });
            }
 
            google.maps.event.addDomListener(window, 'load', init);
        </script>
    </body>
</html>