-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPlot.js
265 lines (229 loc) · 7.62 KB
/
GPlot.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
// Main js file for global itenerary map
function GPlot(map, options) {
this.map_ = map;
options = options || {};
this.clusters_ = [];
this.numClusters_ = 0;
this.points_ = options.points || [];
var latLngMap = {},
clusters = [],
numClusters = 0,
inited = false,
projection,
self = this;
// Store information for the map
var clusterMap = {}, fitleredData = {}, initPlotData = [], mapData, numInFilter, numInitPlot;
var bounds = new google.maps.LatLngBounds();
var map, infowindow;
this.initMap();
/*
* Resets all markers and data and adds markers to map
* given the maps current projection
*/
function addAllMarkers() {
// Remove all markers
var i = 0;
while(i < numClusters) {
clusters[i].clear();
i++;
}
clusters = [];
numClusters = 0;
clusterMap = {};
// Clear all markers
// Member data is keyed by filter category id
var i = 0;
while(i < numInitPlot) {
codeAddress(initPlotData[i]);
i++;
}
}
function codeAddress(data) {
if(data instanceof Array) {
// Go ahead and add items the already have lat long defined
var coded = [], i = 0, info;
while(i < data.length) {
info = data[i];
addLatLong(info, limitToBounds);
i++;
}
} else {
addLatLong(data, limitToBounds);
}
}
/*
* Checks if the data contains valid lat long point
*/
function hasValidPoint(data) {
return (typeof data.latitude != "undefined" && data.longitude != "undefined" && data.latitude != null && data.longitude != null && !isNaN(data.latitude) && !isNaN(data.longitude));
}
function addPoint(e, cat_id) {
codeAddress(mapData[cat_id], true);
}
}
GPlot.prototype.addAllMarkers = function() {
// Remove all markers
var i = 0;
while(i < this.numClusters_) {
this.clusters_[i].clear();
i++;
}
this.clusters_ = [];
this.numClusters_ = 0;
clusterMap = {};
// Clear all markers
// Member data is keyed by filter category id
i = 0;
while(i < this.points_.length) {
this.addLatLong(this.points_[i]);
i++;
}
}
GPlot.prototype.addLatLong = function(point) {
// if(!bounds.contains(point)) return;
// bounds.extend(point);
// Create cluster
var i = 0, cluster, clusters = this.clusters_;
while(i < this.numClusters_) {
cluster = clusters[i];
if(cluster.intersects(point)) {
cluster.addMarker(point);
return;
}
i++;
}
// If not within bounds of any current clusters, add a new one
cluster = new Cluster(this.map_, this.projection_);
cluster.addMarker(point);
clusters.push(cluster);
this.numClusters_ = clusters.length;
}
GPlot.prototype.addPoint = function(point) {
if(data instanceof Array) {
// Go ahead and add items the already have lat long defined
var coded = [], i = 0, info;
while(i < data.length) {
info = data[i];
addLatLong(info, limitToBounds);
i++;
}
} else {
addLatLong(data, limitToBounds);
}
}
GPlot.prototype.initMap = function(latLng) {
var overlay = new google.maps.OverlayView(),
points = this.points_,
self = this;
this.overlay_ = overlay;
var inited = false;
overlay.draw = function() {
if(!inited) {
self.projection_ = overlay.getProjection();
// Loop mapData creating initial data map
var i = 0, numPoints = points.length;
// Loop members for category, if valid points, we push into init plot array that is used to refresh the map
while(i < numPoints) {
self.addAllMarkers();
// initPlotData.push(this.points[i]);
// codeAddress(this.points[i]);
i++;
}
// numInitPlot = initPlotData.length;
// map.fitBounds(bounds);
}
inited = true;
};
overlay.setMap(this.map_);
google.maps.event.addListener(this.map_, "idle", function() {
// bounds = this.map_.getBounds();
self.addAllMarkers();
});
}
GPlot.prototype.plot = function(latLng) {
}
function Cluster(map, projection) {
this.map_ = map;
this.projection_ = projection;
this.plots_ = [];
this.maxZoom_ = 16;
this.numPlots_ = 0;
this.rad_ = 70;
}
Cluster.prototype.addMarker = function(point) {
if(typeof this.marker_ === "undefined") {
this.center_ = this.projection_.fromLatLngToDivPixel(point);
this.marker_ = new ClusterMarker(point, this.map_);
} else if(this.numPlots_ === 1) {
this.marker_.setIcon("cluster.png");
}
this.plots_.push(point);
this.numPlots_ = this.plots_.length;
this.marker_.setCount(this.numPlots_);
}
Cluster.prototype.clear = function(latLng) {
this.marker_.remove();
}
Cluster.prototype.intersects = function(point) {
if(this.map_.getZoom() >= this.maxZoom_) return false;
var pos = this.projection_.fromLatLngToDivPixel(point);
var dx = this.center_.x - pos.x;
var dy = this.center_.y - pos.y;
var dist = Math.sqrt(dx*dx + dy*dy);
return dist <= this.rad_;
}
function ClusterMarker(latLong, map, title, icon, infoWindow, wData) {
this.latLong = latLong;
this.map_ = map;
this.title_ = title;
this.icon_ = icon;
this.setMap(map);
this.count_ = 0;
this.infoWindow_ = infoWindow;
this.windowData_ = wData;
var self = this;
// Add overlay div to map
var div = document.createElement("DIV");
div.style.cssText = "cursor:pointer;"
div.style.background = "url('" + icon + "') no-repeat";
div.style.position = "absolute";
div.style.width = "50px";
div.style.height = "50px";
// div.style.visibility = "hidden";
div.setAttribute("class", "cluster-marker");
var content = "<h1 style='color:#FFF;font-family:Verdana;font-size:11px;line-height:40px;text-align:center;width:38px;'>0</h1>";
div.innerHTML = content;
this.div_ = div;
this.h1_ = div.getElementsByTagName("h1")[0];
this.getPanes().overlayImage.appendChild(div);
this.draw();
this.mapListener_ = google.maps.event.addDomListener(div, "click", function() {
if(self.count_ > 1) {
map.panTo(self.latLong);
map.setZoom(self.map_.getZoom()+1);
} else if(typeof self.infoWindow_ !== "undefined" && typeof self.windowData_ !== "undefined") {
map.panTo(self.latLong);
map.panBy(0, -120);
self.infoWindow_.open(self.latLong, self.windowData_);
}
});
this.setCount = function(count) {
this.count_ = count;
this.h1_.innerHTML = count;
this.h1_.style.visibility = (count <= 1) ? "hidden" : "visible";
}
this.setIcon = function(icon) {
div.style.background = "url('" + icon + "') no-repeat";
}
};
ClusterMarker.prototype = new google.maps.OverlayView;
ClusterMarker.prototype.draw = function() {
var overlayProjection = this.getProjection();
var point = overlayProjection.fromLatLngToDivPixel(this.latLong);
this.div_.style.left = point.x - 18 + "px";
this.div_.style.top = point.y - 50 + "px";
}
ClusterMarker.prototype.remove = function () {
google.maps.event.removeListener(this.mapListener_);
this.div_.parentNode.removeChild(this.div_);
}