You are a lifesaver @geocodezip. I don't have enough rep points to make this a comment however to continue on @goecodezip's answer, my map wouldn't work in association to the autocomplete and found the HTML temple I was using had an invalid <!DOCTYPE HTML> which caused the map to not appear.
To answer your question in the comment @Mamulasa regarding storing the lat and long into your DB, Setup two hidden input fields for lat and lon:
<input type="hidden" id="latitude" name="lat" value="">
<input type="hidden" id="longitude" name="lon" value="">
and in @geocodezip's example above add:
document.getElementById("latitude").value = place.geometry.location.lat();
document.getElementById("longitude").value = place.geometry.location.lng();
within the fillInAddress so this function will now look like this:
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
if (!marker) {
marker = new google.maps.Marker({
map: map,
anchorPoint: new google.maps.Point(0, -29)
});
} else marker.setMap(null);
marker.setOptions({
position: place.geometry.location,
map: map
});
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
//-- ADD LAT AND LON HERE TO APEND TO HIDDEN HTML INPUT
document.getElementById("latitude").value = place.geometry.location.lat();
document.getElementById("longitude").value = place.geometry.location.lng();
// 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;
}
}
}
Wrap the html in a <form method="post"> and you will now be able to obtain all fields including a hidden lat and lon to store in your DB.
Hope this helps.