-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-layer.js
160 lines (121 loc) · 3.57 KB
/
map-layer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @file manages layers for Google Maps, including Clusters
* @author devbab
*/
'use strict';
const { MarkerClusterer } = require("@googlemaps/markerclusterer");
// https://developers.google.com/maps/documentation/javascript/marker-clustering?hl=es
let TheList = new Map();
let TheCluster = null;
let TheClusterLayerName = null; // name of layer used for cluser
// add an object in a list, with associated layer
function add(object, layerName = null) {
if (typeof layerName !== "string") throw (`layer name should be a string`)
let _layer = TheList.get(layerName);
if (!_layer)
TheList.set(layerName, [object]);
else
_layer.push(object);
}
/**
* get array of names matching the name which is string or RegExp
* @param {*} name
* @returns
*/
function _getLayersName(name) {
// name is not a regexp, return simply the name if found in TheList
if (typeof name == "string")
if (TheList.has(name)) return [name];
else return null;
let layersName = Array.from(TheList.keys())
.filter(layerName => {
return layerName.match(name);
});
return layersName;
}
/**
* return array of layers entries
* @param {string | regexp} name name/regex of the layer
* @return array of layer name
*/
function _getLayersContentFromName(name) {
let listNames = _getLayersName(name);
if (!listNames) return null;
const list = listNames.map(name => TheList.get(name));
return list;
}
/**
* Show all items in the layer
* @param {map.google} map - google.map element on which to show the layer
* @param {String} layer - layer name
*/
function show(map, layerName) {
const layers = _getLayersContentFromName(layerName); //return list of Map entries that match layer name
if (!layers) return;
layers.forEach(layer => {
if (!layer) return;
layer.forEach(e => { e.setMap(map) })
})
}
/**
* Show the layer as a clustered layer
* @param {google.map} map map on which to show the layer
* @param {String} layer layer name
*/
function showCluster(map, layerName = null) {
if (typeof name !== "string") throw (`layername is not a string`)
const _layer = TheList.get(layerName);
if (!_layer) return;
// empty if existing
if (TheCluster) TheCluster.clearMarkers();
// Add a marker clusterer to manage the markers.
TheCluster = new MarkerClusterer({
markers: _layer,
map
});
TheClusterLayerName = layerName;
}
function emptyCluster() {
if (TheCluster) {
TheCluster.clearMarkers();
empty(TheClusterLayerName); // delete also markers in the list
TheClusterLayerName = null;
}
}
/**
* Hide the layer
* @param {String} layer name of the layer
*/
function hide(layer = null) {
show(null, layer);
}
/**
* Empty the layer(s)
* @param {String} layer - name of layer ot empty
*/
function empty(name) {
let listNames = _getLayersName(name);
if (!listNames) return;
listNames.forEach(name => {
const layerContent = TheList.get(name);
layerContent.forEach(e => { e.setMap(null) }); // hide each element
TheList.delete(name); // delete the entry
});
}
/**
* returns an array of list of elements
* @param {string | regexp} layer - layer name
* @returns list of list of elements
*/
function list(layerName) {
return _getLayersContentFromName(layerName); //return list of Map entries that match layer name
}
module.exports = {
show: show,
hide: hide,
empty: empty,
add: add,
list: list,
showCluster: showCluster,
emptyCluster: emptyCluster
};