-
Notifications
You must be signed in to change notification settings - Fork 1
/
html5geomap.js
152 lines (115 loc) · 3.75 KB
/
html5geomap.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
HTML5Geomap = {};
HTML5Geomap.engine = "leaflet";
HTML5Geomap.maps = {};
HTML5Geomap.Marker = function(elem) {
// harshly save the Geomap into element DOM
elem._geomap = this
this.elem = elem;
var latlng = [ elem.getAttribute("lat"), elem.getAttribute("lon") ]
switch (HTML5Geomap.engine) {
case "leaflet":
this.obj = L.marker(latlng).bindPopup(elem.innerHTML)
break
default:
console.log("HTML5Geomap.engine not supported")
}
}
HTML5Geomap.Geomap = function(elem) {
// harshly save the Geomap into element DOM
elem._geomap = this
this.elem = elem;
this.mapDiv = null;
this.map = null;
this.mapElems = [];
var initLat = elem.getAttribute("lat")
var initLon = elem.getAttribute("lon")
var initZoomlevel = elem.getAttribute("zoomlevel")
var elems = elem.querySelectorAll("*")
for (var i=0; i<elems.length; i++) {
console.log("ORIGINAL DOM", elems[i])
var elemClass = null;
switch(elems[i].nodeName) {
case "MARKER":
elemClass = HTML5Geomap.Marker;
break;
default:
console.log("unsupported geomap child:", elems[i].nodeName)
break;
}
if (elemClass) {
var mapElem = new elemClass(elems[i]);
this.mapElems.push(mapElem)
}
}
switch (HTML5Geomap.engine) {
case "leaflet":
var shadow = elem.webkitCreateShadowRoot();
var template = document.querySelector("#geomapDivTemplate");
shadow.appendChild(template.content);
shadow.applyAuthorStyles = true; // leak css from the host
mapDiv = shadow.querySelector("div")
this.map = L.map(mapDiv)
this.map.setView([initLat, initLon], initZoomlevel)
// this.map.on('click', function(e) {
// console.log(e.latlng);
// });
// TODO: geolocate if asked
var tileLayer = L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>',
maxZoom: 18
})
tileLayer.addTo(this.map)
// this.map = new HTML5Geomap.LeafletMap(elem, [initLat, initLon], initZoomlevel)
break;
default:
console.log("HTML5Geomap.engine not supported")
}
for (var i=0; i<this.mapElems.length; i++) {
console.log("watlol", this.mapElems[i])
this.mapElems[i].obj.addTo(this.map);
}
// calls func(latlng[lat, lng], originalEvent)
this.on = function(eventType, func) {
this.map.on(eventType, function(e) {
latlng = null
switch (HTML5Geomap.engine) {
case "leaflet":
latlng = [e.latlng.lat, e.latlng.lng]
break
default:
console.log("HTML5Geomap.engine not supported")
}
func(latlng, e)
})
}
this.add = function(obj) {
switch (HTML5Geomap.engine) {
case "leaflet":
obj.obj.addTo(this.map)
obj.obj.openPopup() // TODO DECOUPLE
break;
default:
console.log("HTML5Geomap.engine not supported")
}
}
this.remove = function(obj) {
switch (HTML5Geomap.engine) {
case "leaflet":
this.map.removeLayer(obj.obj)
break;
default:
console.log("HTML5Geomap.engine not supported")
}
}
}
HTML5Geomap.initialize = function(rootElem) {
var html5geomapElems = rootElem.querySelectorAll("geomap")
for (var i=0; i<html5geomapElems.length; i++) {
var geomap = new HTML5Geomap.Geomap(html5geomapElems[i])
// NOTE: this fails
HTML5Geomap.maps[geomap.elem] = geomap
}
}
HTML5Geomap.LeafletMap = function(elem, initView, initZoomlevel) {
}
HTML5Geomap.initialize(document)