-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmietmap.js
107 lines (98 loc) · 2.55 KB
/
mietmap.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
var map;
/*
Compute the arithmetic mean of a list of values.
*/
function mean(values) {
var sum = 0;
for (var index in values) {
sum += values[index];
}
return sum / values.length;
}
/*
Merge data points who have the same coordinates.
The corresponding rent is the average of the merged rents.
*/
function mergeDataPoints(points) {
var merged = {};
for (var index in points) {
var point = points[index];
var key = [point[0], point[1]];
if (key in merged) {
merged[key][1].push(point[2]);
} else {
merged[key] = [[point[0], point[1]], [point[2]]];
}
}
var result = [];
for (var key in merged) {
if (merged.hasOwnProperty(key)) {
var entry = merged[key];
result.push([entry[0][0], entry[0][1], mean(entry[1])]);
}
}
return result;
}
/*
Add circle markers to map.
*/
function addMarkers(json) {
json = mergeDataPoints(json);
for (var index in json) {
var point = json[index];
if (point[2] < MIN_RENT || point[2] > MAX_RENT) {
continue;
}
var fillColor = colormap((point[2] - MIN_RENT) / RENT_SPAN);
var options = $.extend({'fillColor': fillColor}, CIRCLE_OPTIONS);
var label = (point[2].toFixed(2) + ' €/m²').replace(/\./, ',');
L.circleMarker([point[0], point[1]], options)
.setRadius(CIRCLE_RADIUS)
.bindLabel(label)
.addTo(map);
}
}
/*
Add heatmap overlay to map.
*/
function addOverlay() {
L.imageOverlay(OVERLAY_URL, OVERLAY_BOUNDS, OVERLAY_OPTIONS)
.addTo(map);
}
/*
Initialize map.
*/
function initMap() {
var tiles = new L.TileLayer(TILES_URL, {attribution: ATTRIBUTION});
map = new L.Map('map', {
center: INITIAL_LOCATION,
zoom: INITIAL_ZOOM,
maxZoom: MAX_ZOOM,
minZoom: MIN_ZOOM,
zoomControl: false
})
.addLayer(tiles);
L.Control.zoomHome().addTo(map);
addOverlay();
$.getJSON(RENT_URL, addMarkers);
}
/*
Initialize colorbar in legend.
*/
function initColorbar() {
var gradient = colormap_gradient('to right');
$('#colorbar').css('background', gradient);
$('#min-rent').html(MIN_RENT + ' €/m²');
$('#max-rent').html(MAX_RENT + ' €/m²');
}
/*
Initialize legend.
*/
function initLegend() {
var legend = L.control({position: 'bottomright'});
legend.onAdd = function (m) {
return L.DomUtil.get('legend');
};
legend.addTo(map);
initColorbar();
}