I have a webpage where I would like to use both place autocomplete as well as google map with marker. User can search for an address in place autocomplete. Lat-long data for marker comes from DB and this doesn't change. The problem is either map works or place autocomplete, but not both, and the issue is related to callbacks.
From google docs, I've included both callbacks in separate API calls:
<script src="https://maps.googleapis.com/maps/api/js?key=[API KEY]&signed_in=true&libraries=places&callback=initAutocomplete" async defer></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=[API KEY]&callback=initMap"></script>
But this throws error on console, and nothing works.
Error: You have included the Google Maps API multiple times on this page. This may cause unexpected errors.
My question is: How multiple callbacks can be passed to Google API?
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:
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:
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.
You can't add multiple callbacks (and you shouldn't include the API more than once). Put all the code in a single callback.
or see this example in the documentation