Inigo Inigo - 1 year ago 144
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?


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

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


// 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
// [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) {
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 src="[my_key]&signed_in=true&callback=initMap"
async defer></script>

<script src="[my_key]&libraries=places&callback=initAutocomplete"
async defer></script>

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 Source

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

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

  // ... your code

  var initMain = function () {

<script src="[your_key]&signed_in=true&libraries=places&callback=initMain"
    async defer></script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download