-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster.js
More file actions
63 lines (53 loc) · 1.72 KB
/
cluster.js
File metadata and controls
63 lines (53 loc) · 1.72 KB
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
var Cluster = exports;
var sys = require('sys');
var OFFSET = 268435456;
var RADIUS = 85445659.4471;
function lonToX(lon) {
return Math.round(OFFSET + RADIUS * lon * Math.PI / 180);
}
function latToY(lat) {
return Math.round(OFFSET - RADIUS *
Math.log((1 + Math.sin(lat * Math.PI / 180)) /
(1 - Math.sin(lat * Math.PI / 180))) / 2);
}
function pixelDistance(lat1, lon1, lat2, lon2, zoom) {
x1 = lonToX(lon1);
y1 = latToY(lat1);
x2 = lonToX(lon2);
y2 = latToY(lat2);
return Math.sqrt(Math.pow((x1-x2),2) + Math.pow((y1-y2),2)) >> (21 - zoom);
}
/**
* Return an array of clusters based on an array of markers
*
* @function cluster
* @param {Array} markers {Number} distance {Number} zoom
* @return {Array}
*/
Cluster.cluster = function(markers, distance, zoom) {
clustered = []
while (markers.length > 0) {
marker = markers.pop();
cluster = [];
/* Compare against all markers which are left. */
markers.forEach(function(element, index, array){
pixels = pixelDistance(marker.loc.lat, marker.loc.long, element.loc.lat, marker.loc.long, zoom);
if (distance > pixels) {
array.splice(index, 1);
cluster.push(element);
}
});
/* If a marker has been added to cluster, add also the one */
/* we were comparing to and remove the original from array. */
if (cluster.length > 0) {
cluster.push(marker);
clustered.push(cluster);
} else {
clustered.push(marker);
}
}
return clustered;
}
Cluster.getClusters = function(markers, zoom){
return Cluster.cluster(markers, 50, zoom);
}