-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider.js
101 lines (92 loc) · 3.39 KB
/
spider.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
////////////////////////
// custom map spider //
///////////////////////
function isOdd(num) { return num % 2;}
map.addListener('bounds_changed', function() {
var latMedian = [];
var lngMedian = [];
var totalMedian = [];
for (var i=0; i<currentMarkers.length; i++){
if( map.getBounds().contains(currentMarkers[i].getPosition())){
if (typeof currentMarkers[i].offsetLatValue === "undefined"){
currentMarkers[i].offsetLatValue = 0;
currentMarkers[i].offsetLngValue = 0;
}
currentMarkers[i].positionValue = 0;
latMedian.push(currentMarkers[i]);
lngMedian.push(currentMarkers[i]);
totalMedian.push(currentMarkers[i]);
}
}
//12 is the cutoff for minimum number of markers in the current viewport needed to render the spider
if(map.getZoom() === 20 && totalMedian.length > 12){
latMedian.sort(function(a, b) {
var aLat = a.getPosition().lat();
var bLat = b.getPosition().lat();
return ((aLat < bLat) ? -1 : ((aLat > bLat) ? 1 : 0));
});
lngMedian.sort(function(a, b) {
var aLng = a.getPosition().lng();
var bLng = b.getPosition().lng();
return ((aLng < bLng) ? -1 : ((aLng > bLng) ? 1 : 0));
});
$.each(latMedian, function(a, b){
b.positionValue += a;
});
$.each(lngMedian, function(a, b){
b.positionValue += a;
});
totalMedian.sort(function(a, b) {
var aPositionValue = a.positionValue;
var bPositionValue = b.positionValue;
return ((aPositionValue < bPositionValue) ? -1 : ((aPositionValue > bPositionValue) ? 1 : 0));
});
var middle = Math.floor((totalMedian.length - 1) / 2);
var upperCurrentMarkers = totalMedian.slice(middle, -1);
var lowerCurrentMarkers = totalMedian.slice(0, middle - 1);
$.each(upperCurrentMarkers, function(a, b){
if(!b.pinMoved){
if(isOdd(a) === 1){
b.offsetLatValue = (a/64000);
b.offsetLngValue = (-a/64000);
b.pinMoved = true;
}else{
b.offsetLatValue = (a/64000);
b.offsetLngValue = (a/64000);
b.pinMoved = true;
}
var newLatLng = new google.maps.LatLng(
b.getPosition().lat() + b.offsetLatValue,
b.getPosition().lng() + b.offsetLngValue);
b.setPosition(newLatLng);
}
});
$.each(lowerCurrentMarkers, function(a, b){
if(!b.pinMoved){
if(isOdd(a) === 1){
b.offsetLatValue = (-a/64000);
b.offsetLngValue = (a/64000);
b.pinMoved = true;
}else{
b.offsetLatValue = (-a/64000);
b.offsetLngValue = (-a/64000);
b.pinMoved = true;
}
var newLatLng = new google.maps.LatLng(
b.getPosition().lat() + b.offsetLatValue,
b.getPosition().lng() + b.offsetLngValue);
b.setPosition(newLatLng);
}
});
}else if (map.getZoom() !== 20) {
$.each(currentMarkers, function(a, b){
var newLatLng = new google.maps.LatLng(
b.getPosition().lat() - b.offsetLatValue,
b.getPosition().lng() - b.offsetLngValue);
b.setPosition(newLatLng);
b.offsetLngValue = 0;
b.offsetLatValue = 0;
b.pinMoved = false;
})
}
});