Answer the question
In order to leave comments, you need to log in
How to check if there is a need to load google maps on a page?
Hello!
There is a dynamically loaded Google map, which is only needed on the contact page, and I have a single script connected to all pages. When navigating through pages, I periodically get the following error:
You have included the Google Maps API multiple times on this page,
google is not defined,
<script defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA3XfuMJJGzaU8TUItQM7XWD4esZdbpgtA"></script>
<div id="w3docs-map"></div>
function initialize() {
var prop = {
center: new google.maps.LatLng(51.508742, -0.120850),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("w3docs-map"), prop);
}
google.maps.event.addDomListener(window, 'load', initialize);
#w3docs-map {
width:500px;
height:380px;
}
Answer the question
In order to leave comments, you need to log in
function initialize() {
var _map = document.getElementById("w3docs-map");
if(!_map.firstChild){
var prop = {
center: new google.maps.LatLng(51.508742, -0.120850),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(_map, prop);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question