-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_wrapper.js
78 lines (64 loc) · 2.3 KB
/
map_wrapper.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
function MapWrapper(divID, lat, lng, zoom, options) {
MapWrapper.prototype.DEFAULT_CENTER = new GeoPoint(59.93, 30.32)
MapWrapper.prototype.DEFAULT_ZOOM = 13
this.divID = divID
lat = lat || this.DEFAULT_CENTER.lat
lng = lng || this.DEFAULT_CENTER.lng
this.initialCenter = new GeoPoint(lat, lng)
this.zoom = zoom || this.DEFAULT_ZOOM
// override default options by the given ones
this.options = this.DEFAULT_OPTIONS
for (i in options) {
this.options[i] = options[i]
}
this.engines = []
this.activeEngine = null
}
MapWrapper.prototype.DEFAULT_OPTIONS = {
"zoomControl": true,
"moveControl": true,
"typeControl": true, // map type: map, satellite, hybrid
"switchControl": true, // switching between map engines
"scrollToZoom": true // scroll mouse wheel to zoom in/out
}
MapWrapper.prototype.addEngine = function(engine) {
engine.mapWrapper = this
engine.container = this._makeMapDiv(engine.codename + "_" + Math.random + "_id", this.divID)
engine.initialize(engine.container)
engine.setCenter(this.initialCenter)
engine.setZoom(this.zoom)
engine.setOptions(this.options)
engine.container.hide()
// add this engine to the all switch controls of each of previously created engines
for (var i = 0; i < this.engines.length; i++) {
for (var j = 0; j < this.engines[i].switchControls.length; j++) {
this.engines[i].switchControls[j].addEngine(engine)
}
}
this.engines.push(engine)
}
MapWrapper.prototype.selectEngine = function(engine) {
if (this.activeEngine == engine) { return }
if (!this.engines.contains(engine)) { return }
if (this.activeEngine) { // there's no activeEngine initially
engine.setCenter(this.activeEngine.getCenter())
engine.setZoom(this.activeEngine.getZoom())
this.activeEngine.container.hide()
}
engine.container.show()
this.activeEngine = engine
}
MapWrapper.prototype.setCenter = function(lat, lng) {
this.activeEngine.setCenter(new GeoPoint(lat, lng))
}
MapWrapper.prototype.setZoom = function(zoom) {
this.activeEngine.setZoom(zoom)
}
MapWrapper.prototype._makeMapDiv = function(id, mapID) {
var div = document.createElement('div')
div.setAttribute('id', id)
div.style.width = "100%"
div.style.height = "100%"
document.getElementById(mapID).appendChild(div)
return div
}