-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap.js
284 lines (253 loc) · 11.5 KB
/
map.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* jslint browser: true, white: true, sloppy: true, maxerr: 1000 */
/* global L, alerts */
/**
* Extend L.Control.Locate to accomodate the url regex
* @namespace L.Control.Locate - regex to check if the address bar of the browser contains coordinates, needed when we gelocate at startup so we don't override geographic locations in inbound urls
* @see http://stackoverflow.com/a/18690202/2842348
* @author Iain Fraser
* @license MIT stackoverflow
*/
L.Control.CustomLocate = L.Control.Locate.extend({
latlnginURL: function (url) {
return url.match(/([-+]?\d{1,2}[.]\d+)\s*\/\s*([-+]?\d{1,3}[.]\d+)$/);
},
sharedURL: function (url) {
return url.match(/shared/g);
},
});
/**
* Set the map and plugins
*/
var maps = ( function () {
'use strict';
var _tiles = {
// TODO simplify this
"Mapbox Outdoors":
L.tileLayer('https://api.mapbox.com/v4/adriennn.9da931dd/{z}/{x}/{y}.png?access_token=@@mapboxtoken',
{
maxZoom: 20,
minZoom: 2,
reuseTiles: true,
updateWhenZooming: true,
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
}),
"Mapbox Satellite Street":
L.tileLayer('https://api.mapbox.com/styles/v1/adriennn/ciw6qz5tn00002qry747yh58p/tiles/256/{z}/{x}/{y}?access_token=@@mapboxtoken',
{
maxZoom: 20,
minZoom: 2,
reuseTiles: true,
updateWhenZooming: false,
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
}),
"Mapbox Dark":
L.tileLayer('https://api.mapbox.com/styles/v1/adriennn/ciw6qtrg900072pqrevagx9hv/tiles/256/{z}/{x}/{y}?access_token=@@mapboxtoken',
{
maxZoom: 20,
minZoom: 2,
reuseTiles: true,
updateWhenZooming: true,
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
}),
"Stamen Toner":
L.tileLayer('http://{s}.tile.stamen.com/toner-hybrid/{z}/{x}/{y}.png',
{
maxZoom: 20,
minZoom: 2,
reuseTiles: true,
updateWhenZooming: true,
attribution: 'Map design © <a href="http://maps.stamen.com/toner">Stamen</a>',
})
};
var map = L.map('map',
{ zoomControl : false,
attributionControl : true,
zoomSnap : 0,
zoomDelta : 2,
wheelPxPerZoomLevel: 40,
wheelDebounceTime : 200
});
var garbageLayerGroup = L.markerClusterGroup({
spiderfyOnMaxZoom: false,
maxClusterRadius: 50,
disableClusteringAtZoom: 15,
showCoverageOnHover: false,
singleMarkerMode: true
});
var cleaningLayerGroup = L.markerClusterGroup({
spiderfyOnMaxZoom: false,
maxClusterRadius: 80,
disableClusteringAtZoom: 15,
showCoverageOnHover: false,
singleMarkerMode: true,
iconCreateFunction: function (cluster) {
var childCountText = cluster.getChildCount();
return L.divIcon({
html: '<div><span class="leaflet-marker-cluster-count leaflet-marker-cluster-count-cleaning">' + childCountText +
'</span><i class="leaflet-marker-cluster-icon leaflet-marker-cluster-icon-cleaning"></i></div>',
className: 'leaflet-marker-cluster leaflet-marker-cluster-cleaning',
iconSize: [40,40]
});
}
});
var linkLayerGroup = L.markerClusterGroup({
spiderfyOnMaxZoom: false,
maxClusterRadius: 80,
disableClusteringAtZoom: 15,
showCoverageOnHover: false,
singleMarkerMode: true,
iconCreateFunction: function (cluster) {
var childCountText = cluster.getChildCount();
return L.divIcon({
html: '<div><span class="leaflet-marker-cluster-count leaflet-marker-cluster-count-link">' + childCountText +
'</span><i class="leaflet-marker-cluster-icon leaflet-marker-cluster-icon-link"></i></div>',
className: 'leaflet-marker-cluster leaflet-marker-cluster-link',
iconSize: [40,40]
});
}
});
var litterLayerGroup = L.featureGroup();
var areaLayerGroup = L.featureGroup();
var unsavedMarkersLayerGroup = L.featureGroup();
var allLayers = L.layerGroup([
garbageLayerGroup
, areaLayerGroup
, cleaningLayerGroup
, litterLayerGroup
, linkLayerGroup
]);
var hash = L.hash(map, {baseURI: '#/'});
var _overlayGroups = {
"Garbage markers" : garbageLayerGroup
, "Cleaning events" : cleaningLayerGroup
, "Littered coasts and roads" : litterLayerGroup
, "Linked markers" : linkLayerGroup
, "Tiles and areas" : areaLayerGroup
};
var locationcontrol = new L.Control.CustomLocate({
locateOptions: {
enableHighAccuracy: true,
maxZoom: 19
},
position: 'topleft',
icon: 'fa fa-location-arrow',
onLocationError: function (err, control) {
var url = window.location.href;
// console.log('value of this from location error: ', this);
console.log('HREF:', url);
alerts.showAlert(16, "warning", 2000);
console.log('Location error: ', err.message);
console.log('There are coordinates in the url: ', maps.locationcontrol.latlnginURL(url));
console.log('This is a shared url: ', maps.locationcontrol.sharedURL(url));
// If we are currently trying to locate the user and it fails and the maps is already set, just stop the control
if ( maps.locationcontrol.latlnginURL(url) || maps.locationcontrol.sharedURL(url) ) {
control.stop();
}
// Show the world without borders if geolocalization fails
else if ( !maps.locationcontrol.latlnginURL(url) || !maps.locationcontrol.sharedURL(url) ) {
control.stop();
maps.map.setView([0, 0], 2);
}
}
});
var scalecontrol = L.control.scale({metric: true, imperial: false});
var layerscontrol = L.control.layers(_tiles, _overlayGroups, {
// the custom icon 'linkText' is set in Leaflet 1.0.3 source code in L.Control.Layers.__initLayout()
position: 'topleft',
linkText: '<span class="fa fa-fw fa-globe"></span>'
});
var geocodercontrol = L.Control.openCageSearch({key: '@@opencagetoken', limit: 5, position: 'topleft'});
var glomelogincontrol = L.control.login();
var menucontrol = L.control.menu();
var icons = ( function icons () {
var mapMarker = L.DivIcon.extend({
options: {
iconSize: [30, 30],
className: 'map-marker',
html: '<i class="fa fa-fw"></i>'
}
});
var mapmarker = function (options) {
return new mapMarker(options);
};
var genericMarker = mapmarker({className: 'map-marker marker-color-gray marker-generic'})
, garbageMarker = mapmarker({className: 'map-marker marker-garbage'})
, cleaningMarker = mapmarker({className: 'map-marker marker-cleaning marker-color-blue'})
, pastCleaningMarker = mapmarker({className: 'map-marker marker-cleaning-past marker-color-blue'})
, dieoffMarker = mapmarker({className: 'map-marker marker-dieoff'})
, sewageMarker = mapmarker({className: 'map-marker marker-sewage'})
, floatingMarker = mapmarker({className: 'map-marker marker-floating'})
, linkMarker = mapmarker({className: 'map-marker marker-link'})
, cleanedMarker = mapmarker({className: 'map-marker marker-cleaned'});
return { genericMarker : genericMarker
, garbageMarker : garbageMarker
, cleaningMarker : cleaningMarker
, dieoffMarker : dieoffMarker
, sewageMarker : sewageMarker
, floatingMarker : floatingMarker
, linkMarker : linkMarker
, cleanedMarker : cleanedMarker
, pastCleaningMarker : pastCleaningMarker
};
}());
function init () {
_tiles['Mapbox Outdoors'].addTo(maps.map);
//Disable doubleclick to zoom as it might interfer with other map functions
maps.map.doubleClickZoom.disable();
// Add zoom controls on desktop
if ( !window.isMobile ) {
var zoomcontrol = L.control.zoom({position: 'topleft'});
zoomcontrol.options.zoomInText = '<span class="fa fa-fw fa-search-plus"></span>';
zoomcontrol.options.zoomOutText = '<span class="fa fa-fw fa-search-minus"></span>';
zoomcontrol.addTo(maps.map);
}
// Add controls
locationcontrol.addTo(maps.map);
scalecontrol.addTo(maps.map);
layerscontrol.addTo(maps.map);
geocodercontrol.addTo(maps.map);
// Add feature layers
maps.garbageLayerGroup.addTo(maps.map);
maps.areaLayerGroup.addTo(maps.map);
maps.litterLayerGroup.addTo(maps.map);
maps.linkLayerGroup.addTo(maps.map);
maps.cleaningLayerGroup.addTo(maps.map);
maps.unsavedMarkersLayerGroup.addTo(maps.map);
// Add a glome anonymous login button on mobile and small screens
if ( window.isMobile ) {
if ( !maps.map.glomelogincontrol ) {
maps.glomelogincontrol.addTo(maps.map);
}
menucontrol.addTo(maps.map);
}
if ( !maps.locationcontrol.latlnginURL(window.location.href) || !maps.locationcontrol.sharedURL(window.location.href) ) {
console.log('starting to geolocate');
maps.locationcontrol.start();
}
_tiles['Mapbox Outdoors'].on('load', function () {
// Remove the loader div once the tiles have loaded
var loader = document.querySelector('#loader');
if ( loader ) {
document.body.removeChild(loader);
loader = null;
}
});
}
return {
init : init
, map : map
, hash : hash
, locationcontrol : locationcontrol
, glomelogincontrol : glomelogincontrol
, layerscontrol : layerscontrol
, geocodercontrol : geocodercontrol
, garbageLayerGroup : garbageLayerGroup
, areaLayerGroup : areaLayerGroup
, litterLayerGroup : litterLayerGroup
, linkLayerGroup : linkLayerGroup
, cleaningLayerGroup : cleaningLayerGroup
, unsavedMarkersLayerGroup : unsavedMarkersLayerGroup
, allLayers : allLayers
, icons : icons
};
}());