You are lifesaver @geocodezip. I do not have enough reputation points to make this comment, however, to continue @goecodezip's answer, my map will not work due to autocomplete and found that in the HTML temple that I used, there was an invalid <!DOCTYPE HTML> that caused card to not appear.
To answer your question in a comment by @Mamulasa regarding storing lat and long in your database, set 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 the @geocodezip example above add:
document.getElementById ("latitude"). value = place.geometry.location.lat (); document.getElementById ("longitude"). value = place.geometry.location.lng ();
inside fillInAddress, so now this function will 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 <form method="post"> , and now you can get all the fields, including hidden lat and lon, to be stored in your database.
Hope this helps.
Wick 12c
source share