I got this code from this Snippet here that works fine and changed a little bit. But now I wanted to add Marker Clustering to the map like this but I get nothing to work. I added
JavaScript
x
2
1
<script src="https://unpkg.com/@google/markerclustererplus@4.0.1/dist/markerclustererplus.min.js"></script>
2
to the head and
JavaScript
1
3
1
var markerCluster = new MarkerClusterer(markers1,
2
{imagePath:'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
3
in front of “markers1 =
” but then I get the error
JavaScript
1
2
1
Uncaught TypeError: e.getDraggable is not a function
2
I don’t know why this code is not working here without the marker cluster. On my Page, it works fine.
JavaScript
1
103
103
1
var gmarkers1 = [];
2
var markers1 = [];
3
var infowindow = new google.maps.InfoWindow({
4
content: ''
5
});
6
7
// Our markers
8
markers1 = [
9
["0", "***", ***, ***, "1", "red", "-35", "<h3>***<span style='font-size: 10pt;'>( 12345)</span></h3>"],
10
];
11
12
13
/**
14
* Function to init map
15
*/
16
17
function initialize() {
18
var center = new google.maps.LatLng(52.4357808, 4.991315699999973);
19
var mapOptions = {
20
zoom: 6,
21
clickableIcons: false,
22
center: {lat: 50.533481, lng: 10.018343},
23
disableDefaultUI: true,
24
streetViewControl: false,
25
zoomControl: true,
26
gestureHandling: 'greedy',
27
zoomControlOptions: {
28
style: google.maps.ZoomControlStyle.SMALL,
29
position: google.maps.ControlPosition.RIGHT
30
},
31
};
32
33
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
34
for (i = 0; i < markers1.length; i++) {
35
addMarker(markers1[i]);
36
}
37
}
38
39
/**
40
* Function to add marker to map
41
*/
42
43
function addMarker(marker) {
44
var category = marker[4];
45
var title = marker[1];
46
var color = marker[5];
47
var pos = new google.maps.LatLng(marker[2], marker[3]);
48
var content = marker[7];
49
var rotations = marker[6];
50
51
var icon = {
52
path: "M320.69 630.86C320.69 630.86 520.91 323.43 520.91 222.4C520.91 73.71 419.43 9.03 320.46 8.8C221.49 9.03 120 73.71 120 222.4C120 323.43 320.34 630.86 320.34 630.86C320.34 630.86 320.46 630.51 320.46 630.51C320.46 630.63 320.64 630.79 320.69 630.86ZM320.57 144.46C358.97 144.46 390.06 175.54 390.06 213.94C390.06 252.34 358.86 283.43 320.46 283.43C282.17 283.43 251.09 252.34 251.09 213.94C251.09 175.54 282.17 144.46 320.57 144.46Z", //SVG path of awesomefont marker
53
fillColor: color, //color of the marker
54
fillOpacity: 1,
55
strokeWeight: 0,
56
rotation:parseInt(rotations),
57
scale: 0.06, //size of the marker, careful! this scale also affects anchor and labelOrigin
58
anchor: new google.maps.Point(310,620), //position of the icon, careful! this is affected by scale
59
labelOrigin: new google.maps.Point(205,190) //position of the label, careful! this is affected by scale
60
}
61
marker1 = new google.maps.Marker({
62
title: title,
63
position: pos,
64
category: category,
65
map: map,
66
icon:icon
67
});
68
69
70
71
72
gmarkers1.push(marker1);
73
74
// Marker click listener
75
google.maps.event.addListener(marker1, 'click', (function (marker1, content) {
76
return function () {
77
infowindow.setContent(content);
78
infowindow.open(map, marker1);
79
map.panTo(this.getPosition());
80
}
81
})(marker1, content));
82
}
83
var markerCluster = new MarkerClusterer(markers1, {imagePath:'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
84
filterMarkers = function (category) {
85
for (i = 0; i < gmarkers1.length; i++) {
86
marker = gmarkers1[i];
87
// If is same category or category not picked
88
if (marker.category == category || category.length === 0) {
89
//Close InfoWindows
90
marker.setVisible(true);
91
if (infowindow) {
92
infowindow.close();
93
}
94
}
95
// Categories don't match
96
else {
97
marker.setVisible(false);
98
}
99
}
100
}
101
102
// Init map
103
initialize();
JavaScript
1
4
1
#map-canvas {
2
width: 100%;
3
height: 100%;
4
}
JavaScript
1
14
14
1
<head>
2
<script src="https://maps.googleapis.com/maps/api/js?"></script>
3
<script src="https://unpkg.com/@google/markerclustererplus@4.0.1/dist/markerclustererplus.min.js"></script>
4
</head>
5
<body>
6
<div id="map-canvas" style="position: relative; overflow: hidden;"></div>
7
<select id="type" onchange="filterMarkers(this.value);">
8
<option value="">***</option>
9
<option value="1">***</option>
10
<option value="2">***</option>
11
<option value="3">***</option>
12
<option value="4">***</option>
13
</select>
14
</body>
Advertisement
Answer
When I add the specified code to the posted code snippet:
<script src="https://unpkg.com/@google/markerclustererplus@4.0.1/dist/markerclustererplus.min.js"></script>
to the headvar markerCluster = new MarkerClusterer(gmarkers1, {imagePath:'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
to after thegmarkers1
array is available (after the loop callingaddMarker(markers1[i]);
I get the javascript error you report: Uncaught TypeError: e.getDraggable is not a function
That is because the constructor takes three arguments:
JavaScript131var markerCluster = new MarkerClusterer(map, markers,
2{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
3
(your are missing the first map
argument)
Update
two additional issues with the updated code in your question:
- the
MarkerClusterer
constructor is in the wrong place (it is outside theinitialize
function, it runs before the markers are created. - the
markers1
array is the wrong type to add to theMarkerClusterer
, they need to begoogle.maps.Marker
objects (thegmarkers1
array)
working code snippet:
JavaScript
1
108
108
1
var gmarkers1 = [];
2
var markers1 = [];
3
var infowindow = new google.maps.InfoWindow({
4
content: ''
5
});
6
7
// Our markers
8
markers1 = [
9
["0", "Leichlingen", 51.106277, 7.018726, "1", "red", "-35", "<h3>Leichlingen <span style='font-size: 10pt;'>( 42799 )</span></h3><p>Internet: 0 (0%)<p>Social Media: 0 (0%)<p>Dein HSK: 0 (0%)<p>Newsletter: 0 (0%)<p>Banner: (0%)<p>Zeitung: 0 (0%)<p>Bus: 0 (0%)<p>Radio: 0 (0%)<p>Hören Sagen: 0 (0%)<p>vor Ort: (0%)<p>Sonstiges: 1 (100%)"],
10
["1", "Stuttgart", 48.7667, 9.18333, "1", "red", "-35", "<h3>Stuttgart <span style='font-size: 10pt;'>( 70327 )</span></h3><p>Internet: 0 (0%)<p>Social Media: 0 (0%)<p>Dein HSK: 0 (0%)<p>Newsletter: 0 (0%)<p>Banner: (0%)<p>Zeitung: 0 (0%)<p>Bus: 0 (0%)<p>Radio: 0 (0%)<p>Hören Sagen: 1 (100%)<p>vor Ort: (0%)<p>Sonstiges: 0 (0%)"],
11
["2", "Pfaffenhofen", 49.0644444, 8.9763889, "1", "red", "-35", "<h3>Pfaffenhofen <span style='font-size: 10pt;'>( 74397 )</span></h3><p>Internet: 0 (0%)<p>Social Media: 0 (0%)<p>Dein HSK: 0 (0%)<p>Newsletter: 0 (0%)<p>Banner: (0%)<p>Zeitung: 0 (0%)<p>Bus: 0 (0%)<p>Radio: 0 (0%)<p>Hören Sagen: 1 (100%)<p>vor Ort: (0%)<p>Sonstiges: 0 (0%)"],
12
["3", "Bretten", 49.03685, 8.707453, "1", "red", "-35", "<h3>Bretten <span style='font-size: 10pt;'>( 75015 )</span></h3><p>Internet: 0 (0%)<p>Social Media: 2 (13%)<p>Dein HSK: 0 (0%)<p>Newsletter: 0 (0%)<p>Banner: (0%)<p>Zeitung: 4 (27%)<p>Bus: 4 (27%)<p>Radio: 0 (0%)<p>Hören Sagen: 3 (20%)<p>vor Ort: (0%)<p>Sonstiges: 2 (13%)"],
13
["4", "Oberderdingen", 49.0655556, 8.8030556, "1", "red", "-35", "<h3>Oberderdingen <span style='font-size: 10pt;'>( 75038 )</span></h3><p>Internet: 0 (0%)<p>Social Media: 0 (0%)<p>Dein HSK: 0 (0%)<p>Newsletter: 0 (0%)<p>Banner: (0%)<p>Zeitung: 3 (19%)<p>Bus: 1 (6%)<p>Radio: 0 (0%)<p>Hören Sagen: 7 (44%)<p>vor Ort: (0%)<p>Sonstiges: 4 (25%)"],
14
];
15
16
17
/**
18
* Function to init map
19
*/
20
21
function initialize() {
22
var center = new google.maps.LatLng(52.4357808, 4.991315699999973);
23
var mapOptions = {
24
zoom: 6,
25
clickableIcons: false,
26
center: {
27
lat: 50.533481,
28
lng: 10.018343
29
},
30
disableDefaultUI: true,
31
streetViewControl: false,
32
zoomControl: true,
33
gestureHandling: 'greedy',
34
zoomControlOptions: {
35
style: google.maps.ZoomControlStyle.SMALL,
36
position: google.maps.ControlPosition.RIGHT
37
},
38
};
39
40
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
41
42
for (i = 0; i < markers1.length; i++) {
43
addMarker(markers1[i]);
44
}
45
var markerCluster = new MarkerClusterer(map, gmarkers1, {imagePath:'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
46
}
47
48
/**
49
* Function to add marker to map
50
*/
51
52
function addMarker(marker) {
53
var category = marker[4];
54
var title = marker[1];
55
var color = marker[5];
56
var pos = new google.maps.LatLng(marker[2], marker[3]);
57
var content = marker[7];
58
var rotations = marker[6];
59
60
var icon = {
61
path: "M320.69 630.86C320.69 630.86 520.91 323.43 520.91 222.4C520.91 73.71 419.43 9.03 320.46 8.8C221.49 9.03 120 73.71 120 222.4C120 323.43 320.34 630.86 320.34 630.86C320.34 630.86 320.46 630.51 320.46 630.51C320.46 630.63 320.64 630.79 320.69 630.86ZM320.57 144.46C358.97 144.46 390.06 175.54 390.06 213.94C390.06 252.34 358.86 283.43 320.46 283.43C282.17 283.43 251.09 252.34 251.09 213.94C251.09 175.54 282.17 144.46 320.57 144.46Z", //SVG path of awesomefont marker
62
fillColor: color, //color of the marker
63
fillOpacity: 1,
64
strokeWeight: 0,
65
rotation: parseInt(rotations),
66
scale: 0.06, //size of the marker, careful! this scale also affects anchor and labelOrigin
67
anchor: new google.maps.Point(310, 620), //position of the icon, careful! this is affected by scale
68
labelOrigin: new google.maps.Point(205, 190) //position of the label, careful! this is affected by scale
69
}
70
marker1 = new google.maps.Marker({
71
title: title,
72
position: pos,
73
category: category,
74
map: map,
75
icon: icon
76
});
77
78
gmarkers1.push(marker1);
79
80
// Marker click listener
81
google.maps.event.addListener(marker1, 'click', (function(marker1, content) {
82
return function() {
83
infowindow.setContent(content);
84
infowindow.open(map, marker1);
85
map.panTo(this.getPosition());
86
}
87
})(marker1, content));
88
}
89
filterMarkers = function(category) {
90
for (i = 0; i < gmarkers1.length; i++) {
91
marker = gmarkers1[i];
92
// If is same category or category not picked
93
if (marker.category == category || category.length === 0) {
94
//Close InfoWindows
95
marker.setVisible(true);
96
if (infowindow) {
97
infowindow.close();
98
}
99
}
100
// Categories don't match
101
else {
102
marker.setVisible(false);
103
}
104
}
105
}
106
107
// Init map
108
initialize();
JavaScript
1
12
12
1
html,
2
body {
3
width: 100%;
4
height: 100%;
5
padding: 0px;
6
margin: 0px;
7
}
8
9
#map-canvas {
10
width: 100%;
11
height: 95%;
12
}
JavaScript
1
15
15
1
<head>
2
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
3
<script src="https://unpkg.com/@google/markerclustererplus@4.0.1/dist/markerclustererplus.min.js"></script>
4
</head>
5
6
<body>
7
<div id="map-canvas" style="position: relative; overflow: hidden;"></div>
8
<select id="type" onchange="filterMarkers(this.value);">
9
<option value="">***</option>
10
<option value="1">***</option>
11
<option value="2">***</option>
12
<option value="3">***</option>
13
<option value="4">***</option>
14
</select>
15
</body>