forked from arthur-e/Wicket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wicket-gmap3.src.js
378 lines (299 loc) · 11.4 KB
/
wicket-gmap3.src.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
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
/*global Wkt, google, document, window, console*/
/** @license
*
* Copyright (C) 2012 K. Arthur Endsley ([email protected])
* Michigan Tech Research Institute (MTRI)
* 3600 Green Court, Suite 100, Ann Arbor, MI, 48105
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** @preserve
* NOTE: The Google Maps API extension requirest JavaScript 1.6 or higher, due
its dependence on the Array functions map, indexOf, and lastIndexOf
*/
/**
* @augments Wkt.Wkt
* A framework-dependent flag, set for each Wkt.Wkt() instance, that indicates
* whether or not a closed polygon geometry should be interpreted as a rectangle.
*/
Wkt.Wkt.prototype.isRectangle = false;
/**
* @augments Wkt.Wkt
* An object of framework-dependent construction methods used to generate
* objects belonging to the various geometry classes of the framework.
*/
Wkt.Wkt.prototype.construct = {
/**
* Creates the framework's equivalent point geometry object.
* @param config {Object} An optional properties hash the object should use
* @param component {Object} An optional component to build from
* @return {google.maps.Marker}
*/
point: function (config, component) {
var c = component || this.components;
config = config || {};
config.position = new google.maps.LatLng(c[0].y, c[0].x);
return new google.maps.Marker(config);
},
/**
* Creates the framework's equivalent multipoint geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {Array} Array containing multiple google.maps.Marker
*/
multipoint: function (config) {
var i, c, arr;
c = this.components;
config = config || {};
arr = [];
for (i = 0; i < c.length; i += 1) {
arr.push(this.construct.point(config, c[i]));
}
return arr;
},
/**
* Creates the framework's equivalent linestring geometry object.
* @param config {Object} An optional properties hash the object should use
* @param component {Object} An optional component to build from
* @return {google.maps.Polyline}
*/
linestring: function (config, component) {
var i, c;
c = component || this.components;
config = config || {
editable: false
};
config.path = [];
for (i = 0; i < c.length; i += 1) {
config.path.push(new google.maps.LatLng(c[i].y, c[i].x));
}
return new google.maps.Polyline(config);
},
/**
* Creates the framework's equivalent multilinestring geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {Array} Array containing multiple google.maps.Polyline instances
*/
multilinestring: function (config) {
var i, c, arr;
c = this.components;
config = config || {
editable: false
};
config.path = [];
arr = [];
for (i = 0; i < c.length; i += 1) {
arr.push(this.construct.linestring(config, c[i]));
}
return arr;
},
/**
* Creates the framework's equivalent polygon geometry object.
* @param config {Object} An optional properties hash the object should use
* @param component {Object} An optional component to build from
* @return {google.maps.Polygon}
*/
polygon: function (config, component) {
var j, k, c, rings, verts;
c = component || this.components;
config = config || {
editable: false // Editable geometry off by default
};
config.paths = [];
rings = [];
for (j = 0; j < c.length; j += 1) { // For each ring...
verts = [];
// NOTE: We iterate to one (1) less than the Array length to skip the last vertex
for (k = 0; k < c[j].length - 1; k += 1) { // For each vertex...
verts.push(new google.maps.LatLng(c[j][k].y, c[j][k].x));
} // eo for each vertex
if (j !== 0) { // Reverse the order of coordinates in inner rings
if (config.reverseInnerPolygons == null || config.reverseInnerPolygons) {
verts.reverse();
}
}
rings.push(verts);
} // eo for each ring
config.paths = config.paths.concat(rings);
if (this.isRectangle) {
console.log('Rectangles are not yet supported; set the isRectangle property to false (default).');
} else {
return new google.maps.Polygon(config);
}
},
/**
* Creates the framework's equivalent multipolygon geometry object.
* @param config {Object} An optional properties hash the object should use
* @return {Array} Array containing multiple google.maps.Polygon
*/
multipolygon: function (config) {
var i, c, arr;
c = this.components;
config = config || {
editable: false
};
config.path = [];
arr = [];
for (i = 0; i < c.length; i += 1) {
arr.push(this.construct.polygon(config, c[i]));
}
return arr;
}
};
/**
* @augments Wkt.Wkt
* A framework-dependent deconstruction method used to generate internal
* geometric representations from instances of framework geometry. This method
* uses object detection to attempt to classify members of framework geometry
* classes into the standard WKT types.
* @param obj {Object} An instance of one of the framework's geometry classes
* @return {Object} A hash of the 'type' and 'components' thus derived
*/
Wkt.Wkt.prototype.deconstruct = function (obj) {
var features, i, j, multiFlag, verts, rings, tmp;
// google.maps.Marker //////////////////////////////////////////////////////
if (obj.constructor === google.maps.Marker) {
return {
type: 'point',
components: [{
x: obj.getPosition().lng(),
y: obj.getPosition().lat()
}]
};
}
// google.maps.Polyline ////////////////////////////////////////////////////
if (obj.constructor === google.maps.Polyline) {
verts = [];
for (i = 0; i < obj.getPath().length; i += 1) {
tmp = obj.getPath().getAt(i);
verts.push({
x: tmp.lng(),
y: tmp.lat()
});
}
return {
type: 'linestring',
components: verts
};
}
// google.maps.Polygon /////////////////////////////////////////////////////
if (obj.constructor === google.maps.Polygon) {
rings = [];
for (i = 0; i < obj.getPaths().length; i += 1) { // For each polygon (ring)...
tmp = obj.getPaths().getAt(i);
verts = [];
for (j = 0; j < obj.getPaths().getAt(i).length; j += 1) { // For each vertex...
verts.push({
x: tmp.getAt(j).lng(),
y: tmp.getAt(j).lat()
});
}
if (i !== 0) { // Reverse the order of coordinates in inner rings
verts.reverse();
verts.push({ // Add the first coordinate (now at the end, in inner rings) again for closure
x: tmp.getAt(tmp.length - 1).lng(),
y: tmp.getAt(tmp.length - 1).lat()
});
} else {
verts.push({ // Add the first coordinate again for closure
x: tmp.getAt(0).lng(),
y: tmp.getAt(0).lat()
});
}
rings.push(verts);
}
return {
type: 'polygon',
components: rings
};
}
// google.maps.Rectangle ///////////////////////////////////////////////////
if (obj.constructor === google.maps.Rectangle) {
tmp = obj.getBounds();
return {
type: 'polygon',
isRectangle: true,
components: [
[
{ // NW corner
x: tmp.getSouthWest().lng(),
y: tmp.getNorthEast().lat()
},
{ // NE corner
x: tmp.getNorthEast().lng(),
y: tmp.getNorthEast().lat()
},
{ // SE corner
x: tmp.getNorthEast().lng(),
y: tmp.getSouthWest().lat()
},
{ // SW corner
x: tmp.getSouthWest().lng(),
y: tmp.getSouthWest().lat()
},
{ // NW corner (again, for closure)
x: tmp.getSouthWest().lng(),
y: tmp.getNorthEast().lat()
}
]
]
};
}
if (Wkt.isArray(obj)) {
features = [];
for (i = 0; i < obj.length; i += 1) {
features.push(this.deconstruct.call(this, obj[i]));
}
return {
type: (function () {
var k, type = obj[0].constructor;
for (k = 0; k < obj.length; k += 1) {
// Check that all items have the same constructor as the first item
if (obj[k].constructor !== type) {
// If they don't, type is heterogeneous geometry collection
return 'geometrycollection';
}
}
switch (type) {
case google.maps.Marker:
return 'multipoint';
case google.maps.Polyline:
return 'multilinestring';
case google.maps.Polygon:
return 'multipolygon';
default:
return 'geometrycollection';
}
}()),
components: (function () {
// Pluck the components from each Wkt
var i, comps;
comps = [];
for (i = 0; i < features.length; i += 1) {
if (features[i].components) {
comps.push(features[i].components);
}
}
return comps;
}())
};
}
// google.maps.Circle //////////////////////////////////////////////////////
if (obj.getBounds && obj.getRadius) {
// Circle is the only overlay class with both the getBounds and getRadius properties
console.log('Deconstruction of google.maps.Circle objects is not yet supported');
} else {
console.log('The passed object does not have any recognizable properties.');
}
};