Reputation: 31
When I open the page the javascript runs before the button is clicked. It also runs when I do click the button. I don't want it to run until I click the button.
Code:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 45.973, lng: -68.81},
zoom: 10
});
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 50%;
}
<div id="map"></div>
<button id="showLocation" onclick="initMap()">Show Location</button>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDTUe6NaV7fwxOOESwnEdq3YcT9Y2Wr8lg&callback=initMap"
async defer></script>
Thanks in advance!
Upvotes: 1
Views: 249
Reputation: 1370
Remove "&callback=initMap" from the script-src-tag on the last line.
Upvotes: 2
Reputation: 16609
You are passing initMap
as a callback to 'api/js'. This means that when the script loads it will run the callback method, i.e. execute initMap
.
Try removing &callback=initMap
from the URL.
Upvotes: 9
Reputation: 43521
Rename your initMap() to some other name. Google Maps API is calling this function name upon init on the callback.
Upvotes: 2