I am receiving this error when I try to load an HTML page with my map:
Uncaught ReferenceError: google is not defined
I thought it was the order in which I was loading the Google maps API, but I have it at the beginning.
My HTML looks like this:
JavaScript
x
30
30
1
<!doctype html>
2
<html lang="en">
3
<head>
4
<!-- Required meta tags -->
5
<meta charset="utf-8">
6
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7
8
<!-- Bootstrap CSS -->
9
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJT$
10
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
11
<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap"
12
async defer></script>
13
<script src="mapCode.js"></script>
14
15
16
<title>Adventure Map</title>
17
18
</head>
19
<body>
20
<select id="type" onchange="filterMarkers(this.value);">
21
<option value="">All Markers</option>
22
<option value="backpacking">Backpacking</option>
23
<option value="hiking">Hiking</option>
24
<option value="offRoad">Off Road</option>
25
</select>
26
27
<div id="map-canvas" style="width: 100%; height: 600px"></div>
28
29
</body>
30
The mapCode.js that my html code calls looks like this. DO I have thing in the wrong order, is it not loading the google API in time before loading the map?:
JavaScript
1
90
90
1
var gmarkers1 = [];
2
var markers1 = [];
3
var infowindow = new google.maps.InfoWindow({
4
content: ''
5
});
6
var markerCluster;
7
8
// Our markers
9
markers1 = [
10
['0', 'Tanner Trail Grand Canyon', 36.0326, -111.8525, 'backpacking'],
11
['1', 'Yosemite Snow Shoe Badger Pass', 37.6648, -119.6634, 'backpacking'],
12
['2', 'Kayak Camping Catalina Island', 33.3504, -118.3282, 'backpacking'],
13
['3', 'Mini Trans Catalina Trail', 33.3403, -118.3262, 'backpacking']
14
];
15
16
/**
17
* Function to init map
18
*/
19
20
function initialize() {
21
var center = new google.maps.LatLng(52.4357808, 4.991315699999973);
22
var mapOptions = {
23
zoom: 3,
24
center: center,
25
mapTypeId: google.maps.MapTypeId.TERRAIN
26
};
27
28
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
29
for (i = 0; i < markers1.length; i++) {
30
addMarker(markers1[i]);
31
}
32
markerCluster = new MarkerClusterer(map, gmarkers1, {
33
imagePath: 'https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerclustererplus/images/m'
34
});
35
}
36
37
/**
38
* Function to add marker to map
39
*/
40
41
function addMarker(marker) {
42
var category = marker[4];
43
var title = marker[1];
44
var pos = new google.maps.LatLng(marker[2], marker[3]);
45
var content = marker[1];
46
47
marker1 = new google.maps.Marker({
48
title: title,
49
position: pos,
50
category: category,
51
map: map
52
});
53
54
gmarkers1.push(marker1);
55
56
// Marker click listener
57
google.maps.event.addListener(marker1, 'click', (function(marker1, content) {
58
return function() {
59
console.log('Gmarker 1 gets pushed');
60
infowindow.setContent(content);
61
infowindow.open(map, marker1);
62
map.panTo(this.getPosition());
63
//map.setZoom(15);
64
}
65
})(marker1, content));
66
}
67
68
/**
69
* Function to filter markers by category
70
*/
71
72
filterMarkers = function(category) {
73
var newmarkers = [];
74
for (i = 0; i < markers1.length; i++) {
75
marker = gmarkers1[i];
76
// If is same category or category not picked
77
if (marker.category == category || category.length === 0) {
78
marker.setVisible(true);
79
newmarkers.push(marker);
80
}
81
// Categories don't match
82
else {
83
marker.setVisible(false);
84
}
85
}
86
markerCluster.clearMarkers();
87
markerCluster.addMarkers(newmarkers);
88
}
89
google.maps.event.addDomListener(window, "load", initialize);
90
Advertisement
Answer
I think one error is that you need to change
JavaScript
1
2
1
<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap" async defer></script>
2
into
JavaScript
1
2
1
<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initialize" async defer></script>
2
you have no initMap function, but i assume you want to call initialize().