-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgmap-features.js
More file actions
409 lines (367 loc) · 15.6 KB
/
Copy pathgmap-features.js
File metadata and controls
409 lines (367 loc) · 15.6 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* Author: Albert Sun
WSJ.com News Graphics
*/
/*jslint white: false, nomen: false, debug: false, devel: true, onevar: false, plusplus: false, browser: true, bitwise: false, es5: true, maxerr: 200 */
/*global jQuery: false, $: false, log: false, window: false, WSJNG: false, _: false, google: false, localStorage: false */
// Necessary functions
if (!window.typeOf) {
window.typeOf = function(b){var a=typeof b;if(a==="object")if(b){if(b instanceof Array)a="array"}else a="null";return a};
}
// ***************************************
// Just in case there's a console.log hanging around....
// ***************************************
if (!window.console) { window.console = { "log": function() {} }; }
// ***************************************
// Set up a Global Namespace
// ***************************************
var gmap = gmap || {};
(function() {
// Don't want to introduce an underscore dependency just for one function, _.extend
// https://github.com/documentcloud/underscore/
gmap._ = window._ || (function() {
var _ = {};
var breaker = {};
var slice = Array.prototype.slice,
nativeForEach = Array.prototype.forEach;
// The cornerstone, an `each` implementation, aka `forEach`.
// Handles objects with the built-in `forEach`, arrays, and raw objects.
// Delegates to **ECMAScript 5**'s native `forEach` if available.
var each = _.each = _.forEach = function(obj, iterator, context) {
if (obj == null) return;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) {
if (iterator.call(context, obj[key], key, obj) === breaker) return;
}
}
}
};
// Extend a given object with all the properties in passed-in object(s).
_.extend = function(obj) {
each(slice.call(arguments, 1), function(source) {
for (var prop in source) {
if (source[prop] !== void 0) obj[prop] = source[prop];
}
});
return obj;
};
return _;
}());
// extend the Google Maps LatLng object for convenience
google.maps.LatLng.prototype.toGeoJSON = function() { return [this.lng(), this.lat()]; };
google.maps.MVCArray.prototype.toGeoJSON = function() {
var thisarray = this.getArray();
var newarray = [];
for (var i=0,len=thisarray.length; i<len; i++) {
newarray.push(thisarray[i].toGeoJSON());
}
return newarray;
};
/**
* A geometric feature that goes on the map.
* Constructor takes one object params object as an argument.
* Expects there to be the following fields.
* @param {Object} params
* @param {String} params.id The unique identifier of the feature
* @param {Array} params.multipolygon A GeoJSON multipolygon like array with LatLng objects. [ [ [ {google.maps.LatLng}, ], ], ]
* @param {Object} params.fields An object with keys and values of data to store with the feature.
* @param {google.maps.Map} params.map A google maps object onto which to render the polygons of the feature.
* @param {Object} params.controller A controller object to control its selected or not state
*/
gmap.Feature = function(params) {
var self = this;
this.id = params.id;
this.polygons = [];
if (params.fields) {
this.fields = params.fields;
// this.humanized_fields = {};
// for (var prop in params.fields) {
// if (params.fields.hasOwnProperty(prop)) {
// this.humanized_fields[prop] = gmap.util.humanize.addCommas(params.fields[prop]);
// }
// }
}
this.controller = params.controller;
this._selected = false;
this._highlighted = false;
this.highlightCallback = params.highlightCallback;
this.selectCallback = params.selectCallback;
var empty_function = function() { return { }; }
this._responsive_unselected_poly_options = params.responsive_unselected_opts == null ? empty_function : params.responsive_unselected_opts;
this._responsive_highlighted_poly_options = params.responsive_highlighted_opts == null ? empty_function : params.responsive_highlighted_opts;
this._responsive_selected_poly_options = params.responsive_selected_opts == null ? empty_function : params.responsive_selected_opts;
if (params.color) {
this.unselected_poly_options = gmap._.extend({}, this._unselected_poly_options, {"fillColor": params.color});
} else {
this.unselected_poly_options = gmap._.extend({}, this._unselected_poly_options);
}
function mouseoverHandler(e) {
self.setHighlighted(true);
}
function mouseoutHandler(e) {
self.setHighlighted(false);
}
function clickHandler(e) {
if (self.getSelected()) {
self.setSelected(false);
} else {
self.setSelected(true);
}
}
for (var i=0,len=params.multipolygon.length; i<len; i++) {
this.polygons.push( new google.maps.Polygon(gmap._.extend({}, this.unselected_poly_options, this._responsive_unselected_poly_options(), {
paths: params.multipolygon[i],
map: params.map
} )) );
google.maps.event.addListener(this.polygons[i], "mousemove", mouseoverHandler);
google.maps.event.addListener(this.polygons[i], "mouseout", mouseoutHandler);
google.maps.event.addListener(this.polygons[i], "click", clickHandler);
}
};
gmap.Feature.prototype = {
_unselected_poly_options: {
clickable: true,
fillColor: "#AAAAAA",
strokeColor: "#000000",
strokeWeight: 1.0,
strokeOpacity: 0.25
},
_selected_poly_options: {
strokeOpacity: 1.0,
strokeWeight: 1.0,
strokeColor: "#0000FF"
},
_highlighted_poly_options: {
strokeOpacity: 1.0,
strokeWeight: 1.0,
strokeColor: "#00FF00"
},
remove: function(e) {
for (var i=0,len=this.polygons.length; i<len; i++) {
google.maps.event.clearListeners(this.polygons[i], "mousemove");
google.maps.event.clearListeners(this.polygons[i], "mouseout");
this.polygons[i].setMap(null);
}
this.controller = null;
this.polygons = null;
},
toGeoJSON: function() {
var multipoly = [];
for (var i=0,len=this.polygons.length; i<len; i++) {
multipoly.push(this.polygons[i].getPaths().toGeoJSON());
}
return multipoly;
},
// Redraw the polygons associated with the feature
// Highlighted and selected states inherit from unselected state
// NOTE: Remember to set your z-index for your highlights/selects above
// your unselected polygons!
redraw: function() {
var opts = gmap._.extend({}, this.unselected_poly_options, this._responsive_unselected_poly_options());
if(this._highlighted) {
opts = gmap._.extend(opts, this._highlighted_poly_options, this._responsive_highlighted_poly_options());
}
if(this._selected) {
opts = gmap._.extend(opts, this._selected_poly_options, this._responsive_selected_poly_options());
}
for (i=0,len=this.polygons.length; i<len; i++) {
this.polygons[i].setOptions(opts);
}
},
getSelected: function() {
return this._selected;
},
setSelected: function(value) {
var i, len;
if (value === true) {
if (this.controller.selected !== null) {
this.controller.selected.setSelected(false);
}
this._selected = true;
this.controller.selected = this;
this.redraw();
if (this.selectCallback) {
this.selectCallback();
}
} else if (value === false) {
this._selected = false;
this.redraw();
}
},
getHighlighted: function() {
return this._highlighted;
},
setHighlighted: function(value) {
var i,len;
if ((value === true) && (this._highlighted === false)) {
this._highlighted = true;
this.redraw();
if (this.highlightCallback) {
this.highlightCallback();
}
} else if ((value === false) && (this._highlighted === true)) {
this._highlighted = false;
this.redraw();
}
}
};
}());/* Author: Albert Sun
WSJ.com News Graphics
*/
/*jslint white: false, nomen: false, debug: false, devel: true, onevar: false, plusplus: false, browser: true, bitwise: false, es5: true, maxerr: 200 */
/*global jQuery: false, $: false, log: false, window: false, WSJNG: false, _: false, google: false, localStorage: false */
// Necessary functions
if (!window.typeOf) {
window.typeOf = function(b){var a=typeof b;if(a==="object")if(b){if(b instanceof Array)a="array"}else a="null";return a};
}
// ***************************************
// Just in case there's a console.log hanging around....
// ***************************************
if (!window.console) { window.console = { "log": function() {} }; }
// ***************************************
// Set up a Global Namespace
// ***************************************
var gmap = gmap || {};
gmap.geom = gmap.geom || {}; // namespace for utility functions that handle geometry
gmap.geom.ParseGeoJSONMultiPolygon = function(coordinates) {
var i,len1,j,len2,k,len3;
var multipoly = [],poly,linestring;
for (i=0,len1=coordinates.length; i<len1; i++) { //loop through polygons
poly = [];
for (j=0,len2=coordinates[i].length; j<len2; j++) { //loop through linestrings
linestring = [];
for (k=0,len3=coordinates[i][j].length; k<len3; k++) { //loop through points
linestring.push(new google.maps.LatLng(coordinates[i][j][k][1],coordinates[i][j][k][0]));
}
poly.push(linestring);
}
multipoly.push(poly);
}
return multipoly;
};
gmap.geom.ParseGeoJSONPolygon = function(coordinates) {
var j,len2,k,len3;
var poly=[],linestring;
for (j=0,len2=coordinates.length; j<len2; j++) { //loop through linestrings
linestring = [];
for (k=0,len3=coordinates[j].length; k<len3; k++) { //loop through points
linestring.push(new google.maps.LatLng(coordinates[j][k][1],coordinates[j][k][0]));
}
poly.push(linestring);
}
return poly;
};
gmap.geom.ParseKMLMultiPolygon = function(data) {
var polys = $.map($(data).find('Polygon'), function(poly, j) {
var linearrings = $.map($(poly).find("coordinates"), function(line, i) {
var $line = $(line);
var arr = $line.text().split(/\s+/);
var path = $.map(arr, function(el, i) {
if (el !== "") {
var latlng = new google.maps.LatLng(parseFloat($.trim(el).split(',')[1]), parseFloat($.trim(el).split(',')[0]));
//bounds.extend(latlng);
return latlng;
}
});
return [path];
});
//console.log(lines.length);
return [linearrings];
});
return polys;
};
/*jslint white: false, nomen: false, debug: false, devel: true, onevar: false, plusplus: false, browser: true, bitwise: false, es5: true, maxerr: 200 */
/*global jQuery: false, $: false, log: false, window: false, WSJNG: false, _: false, google: false, localStorage: false */
var gmap = gmap || {};
(function() {
function parseKML(kmlstring) {
var doc = $(kmlstring);
var features = $.map(doc.find("Placemark"), function(placemark, i) {
var obj = {};
$placemark = $(placemark);
obj.geometry = $placemark.find("MultiGeometry")[0];
obj.id = $placemark.find("name").text();
obj.properties = {};
var datapoints = $placemark.find("ExtendedData Data"), $datapoint, val;
for (var j=0,len=datapoints.length; j<len; j++) {
$datapoint = $(datapoints[j]),
val = $datapoint.find("value").text();
if (!isNaN(parseFloat(val))) { val = Number(val); }
obj.properties[$datapoint.attr("name")] = val;
}
return obj;
});
return features;
}
gmap.load_polygons = function(params) {
var self = {},
data = params.data,
controller = {"selected": null};
if (params.data_type == "kml") {
data = parseKML(data);
//console.log(data);
} else {
data = data.features;
}
if (params.unselected_opts) {
gmap._.extend(gmap.Feature.prototype._unselected_poly_options, params.unselected_opts);
}
if (params.highlighted_opts) {
gmap._.extend(gmap.Feature.prototype._highlighted_poly_options, params.highlighted_opts);
}
if (params.selected_opts) {
gmap._.extend(gmap.Feature.prototype._selected_poly_options, params.selected_opts);
}
var geom, opts;
for (var i=0,len=data.length; i<len; i++) {
if (typeOf(data[i].geometry.coordinates) !== "array") {
// data is a KML node
geom = gmap.geom.ParseKMLMultiPolygon(data[i].geometry);
} else {
// data is a geom object
if (data[i].geometry.type == "Polygon") {
geom = [ gmap.geom.ParseGeoJSONPolygon(data[i].geometry.coordinates) ];
} else {
geom = gmap.geom.ParseGeoJSONMultiPolygon(data[i].geometry.coordinates);
}
}
opts = {
"id": data[i].id,
"multipolygon": geom,
"fields": data[i].properties,
"controller": controller,
"map": params.map
};
if (params.getColor) {
opts.color = params.getColor(data[i].properties);
}
// Responsive polygon options
opts.responsive_unselected_opts = params.responsive_unselected_opts;
opts.responsive_highlighted_opts = params.responsive_highlighted_opts;
opts.responsive_selected_opts = params.responsive_selected_opts;
// Callbacks
opts.highlightCallback = params.highlightCallback;
opts.selectCallback = params.selectCallback;
self[data[i].id] = new gmap.Feature(opts);
}
return self;
};
/**
* Pass this the a dictionary as returned by load_polygons and it'll remove them from the map.
*/
gmap.remove_polygons = function(features) {
for (var prop in features) {
if (features.hasOwnProperty(prop)) {
features[prop].remove();
delete features[prop];
}
}
return features;
};
}());