-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathmxn.ovi.core.js
More file actions
502 lines (401 loc) · 12.3 KB
/
Copy pathmxn.ovi.core.js
File metadata and controls
502 lines (401 loc) · 12.3 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
mxn.register('ovi', {
Mapstraction: {
init: function(element, api) {
var me = this;
var ovi_map;
var mapLoaded = false;
var eventStates = {
"center": false,
"zoom": false,
"mapsize": false
};
if (ovi.mapsapi) {
ovi_map = new ovi.mapsapi.map.Display (element);
ovi_map.addComponent(new ovi.mapsapi.map.component.InfoBubbles());
ovi_map.addComponent(new ovi.mapsapi.map.component.Behavior());
// Handle click event
ovi_map.addListener('click', function(event){
coords = ovi_map.pixelToGeo(event.targetX, event.targetY);
me.click.fire({'location': new mxn.LatLonPoint(coords.latitude, coords.longitude)});
});
// Handle endPan (via centre change) and zoom events
// the Ovi Maps API doesn't have a discrete event for each of these events
// instead it uses a start/update/end sequence of events, where update may happen
// multiple times or not at all, so we need to keep track of which Ovi events have
// fired during a start(/update) event sequence and then fire the relevent Mapstraction
// events upon receiving the Ovi end event
ovi_map.addListener('mapviewchangestart', function(event){
if (event.data & event.MAPVIEWCHANGE_CENTER) {
eventStates.center = true;
}
if (event.data & event.MAPVIEWCHANGE_ZOOM) {
eventStates.zoom = true;
}
if (event.data & event.MAPVIEWCHANGE_SIZE) {
eventStates.mapsize = true;
}
});
ovi_map.addListener('mapviewchangeupdate', function(event){
if (event.data & event.MAPVIEWCHANGE_CENTER) {
eventStates.center = true;
}
if (event.data & event.MAPVIEWCHANGE_ZOOM) {
eventStates.zoom = true;
}
if (event.data & event.MAPVIEWCHANGE_SIZE) {
eventStates.mapsize = true;
}
});
ovi_map.addListener('mapviewchangeend', function(event){
// The Ovi Maps API doesn't support a "map loaded" event, but both a
// "centre" and "size" mapviewchangestart/mapviewchangeupdate/mapviewchangeend
// event sequence will be fired as part of the initial loading so we can trap
// this and fire the MXN "load" event.
if (!mapLoaded) {
if (eventStates.center && eventStates.mapsize) {
mapLoaded = true;
eventStates.mapsize = false;
me.load.fire();
}
}
else {
if (eventStates.center) {
eventStates.center = false;
me.moveendHandler(me);
me.endPan.fire();
}
}
if (eventStates.zoom) {
eventStates.zoom = false;
me.changeZoom.fire();
}
});
this.maps[api] = ovi_map;
this.loaded[api] = true;
}
else {
alert(api + ' map script not imported');
}
},
applyOptions: function() {
var map = this.maps[this.api];
if (this.options.enableScrollWheelZoom) {
map.addComponent(new ovi.mapsapi.map.component.zoom.MouseWheel());
}
else {
var mousewheel = map.getComponentById('zoom.MouseWheel');
if (mousewheel) {
map.removeComponent(mousewheel);
}
}
},
resizeTo: function(width, height) {
this.currentElement.style.width = width;
this.currentElement.style.height = height;
},
addControls: function(args) {
/* args = {
* pan: true,
* zoom: 'large' || 'small',
* overview: true,
* scale: true,
* map_type: true,
* }
*/
var map = this.maps[this.api];
if (args.pan) {
map.addComponent(new ovi.mapsapi.map.component.Behavior());
}
// TODO: The Ovi Maps API doesn't currently differentiate between large and small
// style of Zoom controls so, for now, make them functionally equivalent
if (args.zoom == 'large' || args.zoom == 'small') {
map.addComponent(new ovi.mapsapi.map.component.ZoomBar());
}
if (args.overview) {
map.addComponent(new ovi.mapsapi.map.component.Overview());
}
if (args.scale) {
map.addComponent(new ovi.mapsapi.map.component.ScaleBar ());
}
if (args.map_type) {
map.addComponent(new ovi.mapsapi.map.component.TypeSelector ());
}
},
// TODO: The Ovi Maps API doesn't currently differentiate between large and small
// style of Zoom controls so, for now, make them functionally equivalent
addSmallControls: function() {
var map = this.maps[this.api];
map.addComponent(new ovi.mapsapi.map.component.ZoomBar());
},
addLargeControls: function() {
var map = this.maps[this.api];
map.addComponent(new ovi.mapsapi.map.component.ZoomBar());
},
addMapTypeControls: function() {
var map = this.maps[this.api];
map.addComponent(new ovi.mapsapi.map.component.TypeSelector ());
},
setCenterAndZoom: function(point, zoom) {
var map = this.maps[this.api];
var pt = point.toProprietary(this.api);
map.setCenter(pt);
map.setZoomLevel(zoom);
},
addMarker: function(marker, old) {
var map = this.maps[this.api];
var ovi_marker = marker.toProprietary(this.api);
map.objects.add(ovi_marker);
return ovi_marker;
},
removeMarker: function(marker) {
var map = this.maps[this.api];
map.objects.remove(marker.proprietary_marker);
},
declutterMarkers: function(opts) {
throw 'Not supported';
},
addPolyline: function(polyline, old) {
var map = this.maps[this.api];
var ovi_polyline = polyline.toProprietary(this.api);
map.objects.add(ovi_polyline);
return ovi_polyline;
},
removePolyline: function(polyline) {
var map = this.maps[this.api];
map.objects.remove(polyline.proprietary_polyline);
},
getCenter: function() {
var map = this.maps[this.api];
return new mxn.LatLonPoint(map.center.latitude, map.center.longitude);
},
setCenter: function(point, options) {
var map = this.maps[this.api];
var pt = point.toProprietary(this.api);
map.setCenter(pt);
},
setZoom: function(zoom) {
var map = this.maps[this.api];
map.setZoomLevel(zoom);
},
getZoom: function() {
var map = this.maps[this.api];
return map.zoomLevel;
},
getZoomLevelForBoundingBox: function(bbox) {
var map = this.maps[this.api];
var sw = bbox.getSouthWest().toProprietary(this.api);
var ne = bbox.getNorthEast().toProprietary(this.api);
var ovi_bb = new ovi.mapsapi.geo.BoundingBox(sw, ne);
return map.getBestZoomLevel(ovi_bb);
},
setMapType: function(type) {
var map = this.maps[this.api];
switch (type) {
case mxn.Mapstraction.ROAD:
map.set("baseMapType", map.NORMAL);
break;
case mxn.Mapstraction.PHYSICAL:
map.set("baseMapType", map.TERRAIN);
break;
case mxn.Mapstraction.HYBRID:
throw 'Not implemented';
case mxn.Mapstraction.SATELLITE:
map.set("baseMapType", map.SATELLITE);
break;
default:
map.set("baseMapType", map.NORMAL);
break;
} // end-switch ()
},
getMapType: function() {
var map = this.maps[this.api];
var type = map.baseMapType;
switch(type) {
case map.NORMAL:
return mxn.Mapstraction.ROAD;
case map.TERRAIN:
return mxn.Mapstraction.PHYSICAL;
case map.SATELLITE:
return mxn.Mapstraction.SATELLITE;
default:
return null;
} // end-switch ()
},
getBounds: function() {
var map = this.maps[this.api];
var bbox = map.getViewBounds();
var nw = bbox.topLeft;
var se = bbox.bottomRight;
return new mxn.BoundingBox(se.latitude, nw.longitude, nw.latitude, se.longitude);
},
setBounds: function(bounds) {
var map = this.maps[this.api];
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
var nw = new mxn.LatLonPoint(ne.lat, sw.lon).toProprietary(this.api);
var se = new mxn.LatLonPoint(sw.lat, ne.lon).toProprietary(this.api);
var ovi_bb = new ovi.mapsapi.geo.BoundingBox(nw, se);
var keepCentre = false;
map.zoomTo(ovi_bb, keepCentre);
},
addImageOverlay: function(id, src, opacity, west, south, east, north, oContext) {
throw 'Not implemented';
},
setImagePosition: function(id, oContext) {
throw 'Not implemented';
},
addOverlay: function(url, autoCenterAndZoom) {
throw 'Not implemented';
},
addTileLayer: function(tile_url, opacity, copyright_text, min_zoom, max_zoom, map_type) {
throw 'Not implemented';
},
toggleTileLayer: function(tile_url) {
throw 'Not implemented';
},
getPixelRatio: function() {
throw 'Not implemented';
},
mousePosition: function(element) {
var map = this.maps[this.api];
var locDisp = document.getElementById(element);
var coords;
if (locDisp !== null) {
map.addListener('mousemove', function(event){
coords = map.pixelToGeo(event.targetX, event.targetY);
locDisp.innerHTML = coords.latitude.toFixed(4) + ' / ' + coords.longitude.toFixed(4);
});
locDisp.innerHTML = '0.0000 / 0.0000';
}
}
},
LatLonPoint: {
toProprietary: function() {
return new ovi.mapsapi.geo.Coordinate(this.lat, this.lon);
},
fromProprietary: function(oviCoordinate) {
this.lat = oviCoordinate.latitude;
this.lon = oviCoordinate.longitude;
}
},
Marker: {
toProprietary: function() {
var properties = [];
var self = this;
if (this.iconAnchor) {
properties.anchor = [this.iconAnchor[0], this.iconAnchor[1]];
}
if (this.iconUrl) {
properties.icon = this.iconUrl;
}
this.proprietary_infobubble = null;
var prop_marker = new ovi.mapsapi.map.Marker(
self.location.toProprietary('ovi'),
properties);
if (this.infoBubble) {
var event_action = "click";
if (this.hover) {
event_action = "mouseover";
}
prop_marker.addListener(event_action, function() {
self.openBubble();
});
}
if (this.draggable) {
prop_marker.enableDrag();
prop_marker.addListener("dragstart", function(event){
var bc = self.map.getComponentById("InfoBubbles");
if (bc.bubbleExists(self.proprietary_infobubble)) {
self.closeBubble();
prop_marker.set("restore_infobubble", true);
}
}, false);
prop_marker.addListener("dragend", function(event){
var xy = event.dataTransfer.getData("application/map-drag-object-offset");
var new_coords = self.map.pixelToGeo(
event.displayX - xy.x + prop_marker.anchor.x,
event.displayY - xy.y + prop_marker.anchor.y
);
var bb = self.map.getBoundingBox();
if (bb.contains(new_coords)) {
self.location.lat = new_coords.latitude;
self.location.lon = new_coords.longitude;
}
if (prop_marker.get("restore_infobubble")) {
prop_marker.set("restore_infobubble", false);
self.openBubble();
}
}, false);
}
return prop_marker;
},
openBubble: function() {
if (!this.map) {
throw 'This marker must be added to a map in order to manage a Bubble';
}
this.proprietary_infobubble = this.map.getComponentById("InfoBubbles").addBubble(this.infoBubble, this.location.toProprietary('ovi'));
},
closeBubble: function() {
if (!this.map) {
throw 'This marker must be added to a map in order to manage a Bubble';
}
if (this.map.getComponentById("InfoBubbles").bubbleExists(this.proprietary_infobubble)) {
this.map.getComponentById("InfoBubbles").removeBubble(this.proprietary_infobubble);
}
this.proprietary_infobubble = null;
},
hide: function() {
this.proprietary_marker.set('visibility', false);
},
show: function() {
this.proprietary_marker.set('visibility', true);
},
update: function() {
throw 'Not implemented';
},
updateIcon: function(iconUrl, iconSize) {
throw 'Not implemented';
},
updateLocation: function(point) {
throw 'Not implemented';
}
},
Polyline: {
toProprietary: function() {
var coords = [];
for (var i=0, length=this.points.length; i<length; i++) {
coords.push(this.points[i].toProprietary('ovi'));
}
if (this.closed || coords[0].equals(coords[length-1])) {
var polycolor = new mxn.util.Color();
polycolor.setHexColor(this.color || "#5462E3");
var polycolor_rgba = "rgba(" + polycolor.red + "," + polycolor.green + "," +
polycolor.blue + "," + (this.opacity || 1.0) + ")";
var polygon_options = {
'visibility' : true,
'fillColor' : polycolor_rgba,
'color' : this.color || "#5462E3",
'stroke' : 'solid',
'width' : this.width || 1
};
this.proprietary_polyline = new ovi.mapsapi.map.Polygon (coords, polygon_options);
}
else {
var polyline_options = {
'visibility' : true,
'color' : this.color || "#5462E3",
'stroke' : 'solid',
'width' : this.width || 1
};
this.proprietary_polyline = new ovi.mapsapi.map.Polyline (coords, polyline_options);
}
return this.proprietary_polyline;
},
show: function() {
this.proprietary_polyline.set('visibility', true);
},
hide: function() {
this.proprietary_polyline.set('visibility', false);
}
}
});