This short code snippet is about getting the latitude and longitude of the selected address from places autocomplete.
Note – Don’t forget to include the following script tag in your webpage before the given code snippet.
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY_HERE&libraries=places"></script>
function getPlace(){
const input = document.getElementById("fieldID");
const options = {
//add options here if you want more customizations
};
const autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
let place = autocomplete.getPlace();
console.log(place.geometry.location.lat());
console.log(place.geometry.location.lng());
});
}
window.addEventListener('load', getPlace);
Need Professional Support for Your Website Problems?
Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.
Hire Us