Inigo Inigo - 7 days ago 5
Javascript Question

How do I call google maps api multiple times in order to load different functions?

I want to call google maps api twice in order to call functions "initMap" and "initAutocomplete". It is not allowed though to call google maps api multiple times. How can I make it work?

<body>

<div id="floating-panel">
<input id="address" type="textbox" value="Copenhagen">
<input id="submit" type="button" value="Search">
</div>
<div id="map"></div>

<div id="locationField">
<input id="autocomplete" placeholder="Enter your address"
onFocus="geolocate()" type="text"></input>
</div>

<script>

// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.
function initAutocomplete() {
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete((document.getElementById('autocomplete')),{types: ['geocode']});
// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', fillInAddress);
}

function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();

for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}

// 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;
}
}
}

// [START region_geolocation]
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
// [END region_geolocation]

function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: {lat: 55.70, lng: 12.6}
});
var geocoder = new google.maps.Geocoder();

document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
}

function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}

</script>

<script src="https://maps.googleapis.com/maps/api/js?key=[my_key]&signed_in=true&callback=initMap"
async defer></script>

<script src="https://maps.googleapis.com/maps/api/js?key=[my_key]&libraries=places&callback=initAutocomplete"
async defer></script>
</body>


As you can see I'm calling it twice in the bottom of the script. Is there a way to call both functions calling google maps only once? Thanks!

Answer

You can specify a single callback to load both your init functions.

Define a callback entry point, then call your two inits:

<script>
  // ... your code

  var initMain = function () {
    initMap();
    initAutocomplete();
  };
</script>

<script src="https://maps.googleapis.com/maps/api/js?key=[your_key]&signed_in=true&libraries=places&callback=initMain"
    async defer></script>