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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/* layers */
var default_pointer = "/media/images/default-pointer.png";
var marker_cluster = "/media/images/marker-cluster.png";
var view_projection = 'EPSG:3857';
var map_default_center = 'SRID=4326;POINT (2.4397 46.5528)';
var map_default_zoom = '7';
var source_osm = function(options){
return new ol.layer.Tile({
source: new ol.source.OSM()
});
};
var default_map_layers = {
'osm': source_osm
};
var get_layers = function(layers){
if (!layers){
layers = [{'type': 'osm', 'options': null}];
}
var ol_layers = [];
for (idx in layers){
var layer_attr = layers[idx];
ol_layers.push(
default_map_layers[layer_attr['type']](layer_attr['options'])
);
}
return ol_layers;
};
/* get markers */
var get_markers = function(points){
};
/* styles */
var get_style = function(feature){
return new ol.style.Style({
image: new ol.style.Icon({
anchor: [17, 50],
anchorXUnits: 'pixels',
anchorYUnits: 'pixels',
size: [35, 50],
src: static_path + default_pointer
})
});
};
/* clustering */
var invisible_style_icon;
var _styleCache;
var _remindOldStyle;
var _remindUpdated;
var _currentRemind;
var _revision = 1;
var cluster_source;
var cluster_layer;
var enable_clustering = function(){
if (!invisible_style_icon){
invisible_style_icon = new ol.style.Style({
image: new ol.style.Icon({
//still need something even if it's invisible
src : static_path + marker_cluster,
opacity : 0
})
});
}
// Style clusters and hide items inside clusters
_styleCache = {};
_remindOldStyle = {};
_remindUpdated = {};
_currentRemind = -1;
function cluster_get_style (feature, resolution){
feature.set('key', 'cluster');
var features = feature.get('features');
var size = features.length;
feature.set('size', size);
var style = _styleCache[size];
// no cluster for lonely marker
if (!style && size > 1){
var color = size > 25 ? "192,0,0" : size > 8 ? "255,128,0" : "0,128,0";
var radius = Math.max(8, Math.min(size * 0.75, 20));
var lbl = size.toString();
style = _styleCache[size] = [
new ol.style.Style({
image: new ol.style.Circle({
radius: radius,
stroke: new ol.style.Stroke({
color:"rgba("+color+",0.5)",
width:15
}),
fill: new ol.style.Fill({
color:"rgba("+color+",1)"
})
}),
text: new ol.style.Text({
text: lbl,
fill: new ol.style.Fill({
color: '#fff'
})
})
})
];
}
// don't reapply the style when no modif have been opered
if (_currentRemind != _revision){
_remindUpdated = [];
_currentRemind = _revision;
}
if (size > 1){
// marker himself disappear
for (idx in features){
var feat = features[idx];
if (!_remindUpdated[feat.getProperties()['id']]){
if (!_remindOldStyle[feat.getProperties()['id']]){
_remindOldStyle[feat.getProperties()['id']] = feat.getStyle();
}
feat.setStyle(invisible_style_icon);
_remindUpdated[feat.getProperties()['id']] = 1;
}
}
} else {
// or re-appear
var feat = features[0];
if (!_remindUpdated[feat.getProperties()['id']] &&
_remindOldStyle[feat.getProperties()['id']]){
feat.setStyle(_remindOldStyle[feat.getProperties()['id']]);
_remindUpdated[feat.getProperties()['id']] = 1;
}
}
return style;
}
// Cluster Source
cluster_source = new ol.source.Cluster({
distance: 40,
source: new ol.source.Vector()
});
// Animated cluster layer
cluster_layer = new ol.layer.Vector({
name: 'Cluster',
source: cluster_source,
// Cluster style
style: cluster_get_style
});
map.addLayer(cluster_layer);
};
var reinit_clustering = function(){
cluster_source.getSource().clear();
_styleCache = {};
_remindOldStyle = {};
_remindUpdated = {};
_currentRemind = -1;
};
/* display map */
var vector_source;
var vector_layer;
var center;
var point_features;
var map;
var map_view;
var map_layers;
var proj_options = {
dataProjection:'EPSG:4326', featureProjection: view_projection
}
var geojson_format = new ol.format.GeoJSON(proj_options);
var wkt_format = new ol.format.WKT(proj_options);
var initialize_map = function(map_id, layers){
center = wkt_format.readGeometry(map_default_center).getCoordinates();
/*
vector_source = new ol.source.Vector({
features: geojson_format.readFeatures(points)
});
*/
vector_source = new ol.source.Vector();
vector_layer = new ol.layer.Vector({
source: vector_source,
style: get_style
});
map_layers = get_layers(layers);
map_layers.push(vector_layer);
map_view = new ol.View({
projection: view_projection,
center: ol.proj.fromLonLat([center[0], center[1]]),
zoom: map_default_zoom
});
map = new ol.Map({
target: map_id,
layers: map_layers,
view: map_view
});
}
var redraw_map = function(map_id, layers){
map.setTarget(null);
map = null;
initialize_map(map_id, layers);
};
var display_map = function(map_id, points, layers){
if (map){
redraw_map(map_id, layers);
} else {
initialize_map(map_id, layers);
}
point_features = geojson_format.readFeatures(points);
vector_source.clear();
vector_source.addFeatures(point_features);
map.updateSize();
if (points.features.length){
map_view.fit(vector_source.getExtent());
if (map_view.getZoom() > 12){
map_view.setZoom(12);
}
}
enable_clustering();
cluster_source.getSource().addFeatures(point_features);
}
|