-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
298 lines (251 loc) · 6.2 KB
/
index.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'use strict';
var colors = {
'DIY': '#114477',
'RET': '#4477AA',
'DRG': '#77AADD',
'FRN': '#117755',
'FIN': '#44AA88',
'COM': '#99CCBB',
'EAT': '#777711',
'PHA': '#AAAA44',
'KFZ': '#DDDD77',
'CLO': '#771111',
'FOD': '#AA4444',
'LEH': '#DD7777',
'TVL': '#771144',
'LSR': '#AA4477',
'GAS': '#DD77AA'
};
var poiLayer;
var poiData;
var csvRows;
var filter;
var boostType = 'ball';
var boostScale = 1.5;
var boostExp = 0.125;
// set up the map
var mapLocation = new L.LatLng(51.5, 10);
// create a map in the "map" div, set the view to a given place and zoom
var map = new L.Map('map', {
preferCanvas: true // marker booster needs the canvas
}).setView(mapLocation, 6);
// This is my personal api key. Please get your own free key at https://developer.myptv.com/
var apiKey = 'YjA2OGIzOGMxMjdiNGZkOGIwYTMwNmI0OGE4Y2ZiMTc6Y2QwZjUyMDYtMTk2Ni00N2NlLWFmNTYtNzBjOWZjZTgxNzJh';
L.tileLayer('https://api.myptv.com/rastermaps/v1/image-tiles/{z}/{x}/{y}?style={style}&apiKey={apiKey}', {
maxZoom: 22,
attribution: '© ' + new Date().getFullYear() + ' PTV Group, HERE',
style: 'silica',
apiKey: apiKey
}).addTo(map);
// add scale control
L.control.scale().addTo(map);
// add input control
var info = L.control();
info.onAdd = function (map) {
var container = document.getElementById('panel-control')
L.DomEvent.disableClickPropagation(container);
L.DomEvent.disableScrollPropagation(container);
return container;
};
info.addTo(map);
// var circle = L.circle([51.508, -0.11], {
// color: 'red',
// fillColor: '#f03',
// fillOpacity: 0.5,
// radius: 50000
// }).addTo(map);
// circle.bindPopup("I am a circle!");
var legend = L.control({
position: 'bottomright'
});
ssv('https://rawgit.com/oliverheilig/leaflet-marker-booster/master/data/inobas-slim.csv',
function (rows) {
initialize(rows);
});
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info lll');
var str = '';
var i =0;
str +='<div class="pure-g">';
for (var key in colors) {
if (colors.hasOwnProperty(key)) {
str +=
'<div class="pure-u-1-3"><i style="background:' + colors[key] + '"></i> ' + key + '</div>';
if((i+1) %3 === 0 && i > 0 && i < Object.keys(colors).length-1)
str +='</div><div class="pure-g">';
i++;
}
}
str +='</div>';
div.innerHTML = str;
return div;
};
legend.addTo(map);
// IE
if (!String.prototype.startsWith) {
String.prototype.startsWith = function (searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}
// IE8
if (!Array.prototype.filter) {
Array.prototype.filter = function (fun /*, thisp */ ) {
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}
function initialize(rows) {
csvRows = rows;
poiData = createJsonFromRows(csvRows);
if (filter) {
poiData.features = poiData.features.filter(function (d) {
return d.properties.category === filter;
});
}
// tip: sort the features by latitue, so they overlap nicely on the map!
poiData.features.sort(function (a, b) {
return b.geometry.coordinates[1] - a.geometry.coordinates[1];
});
resetPoiLayer();
}
function resetPoiLayer() {
if (poiLayer)
map.removeLayer(poiLayer);
poiLayer = L.geoJson(poiData, {
attribution: 'DDS, Inobas',
pointToLayer: poiStyle
}).addTo(map);
}
function poiStyle(feature, latlng) {
var style =
L.circleMarker([feature.geometry.coordinates[1], feature.geometry.coordinates[0]], {
fillColor: colors[feature.properties.category],
fillOpacity: 1,
stroke: true,
color: '#111',
weight: 1,
boostType: boostType,
boostScale: boostScale,
boostExp: boostExp
}).setRadius(6);
var html = feature.properties.description;
if (feature.properties.www) {
var hRef = feature.properties.www;
if (!hRef.startsWith('http'))
hRef = 'http://' + hRef;
html = html + '<br><a target="_blank" href="' + hRef + '">' + feature.properties.www + '</a>';
}
style.bindPopup(html);
return style;
}
function setBoostType(method) {
switch (method) {
case 1:
boostType = 'circle';
break;
case 2:
boostType = 'ball';
break;
case 3:
boostType = 'balloon';
break;
default:
boostType = undefined;
}
resetPoiLayer();
}
function setScaleMode(method) {
switch (method) {
case 0:
boostScale = 1;
boostExp = 0;
break;
case 1:
boostScale = 1.5;
boostExp = 0.125;
break;
case 2:
boostScale = 3;
boostExp = 0.25;
break;
default:
boostScale = 6;
boostExp = 0.325;
}
resetPoiLayer();
}
function filterPois() {
var e = document.getElementById('type');
var value = e.options[e.selectedIndex].value;
if (value === '---')
filter = null;
else
filter = value;
initialize(csvRows);
}
/**
* Convert a PTV Mercator coordinate to WGS84
* @param {number} x x-coordinate
* @param {number} y y-coordinate
* @returns {Number|Array} the uprojected WGC coordinate
*/
function ptvMercatorToWgs(x, y) {
// PTV Mercator is actually Google/Web Mercator with a different scale
var ptvToGoogle = L.Projection.SphericalMercator.R / 6371000;
// use the Leaflet SphericalMercator projection with scaled input coordinates
var p = L.Projection.SphericalMercator.unproject(
L.point(x, y).scaleBy(L.point(ptvToGoogle, ptvToGoogle)));
return [p.lng, p.lat];
}
/**
* Loads a semicolon separated text file
* @param {} url
* @param {} callback
*/
function ssv(url, callback) {
var ssvParse = d3.dsvFormat(';');
d3.request(url)
.mimeType('text/csv')
.response(function (xhr) {
return ssvParse.parse(xhr.responseText);
})
.get(callback);
}
function createJsonFromRows(rows) {
var json = {
type: 'FeatureCollection'
};
json.features = rows.map(function (d) {
var feature = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: ptvMercatorToWgs(d.X, d.Y)
},
properties: {
id: d.ID,
category: d.CATEGORY,
www: d.WWW,
description: d.NAME
}
};
return feature;
});
return json;
}