forked from thelmanews/th-mapbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlens-viz-mapbox.html
238 lines (205 loc) · 7.5 KB
/
lens-viz-mapbox.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../th-animated/th-animated.html">
<link rel="import" href="../core-ajax/core-ajax.html">
<link rel="import" href="../th-theme/th-theme.html">
<!--
Element providing the ability to create maps with the Mapbox API.
##### Example
<lens-viz-mapbox
animateOnInit="true"
mapId="examples.map-i87786ca"
layers="[{'layerId': 'examples.bike-lanes'},{'layerId': 'examples.bike-locations'}]"
startingLat="38.8922"
startingLng="-77.0348"
></lens-viz-mapbox>
@element lens-viz-mapbox
@blurb Element providing the ability to create maps with the Mapbox API.
@status alpha
@homepage http://github.com/thelmanews/lens-viz-mapbox/demo.html
-->
<polymer-element name="lens-viz-mapbox" extends="th-animated" attributes="accessTokenRoute accessToken mapId layers geolocation geolocateZoom startingZoom startingLat startingLng showSwipeBar input markerColor">
<template>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.2/mapbox.css' rel='stylesheet' />
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-locatecontrol/v0.24.0/L.Control.Locate.css' rel='stylesheet' />
<core-style ref="theme"></core-style>
<style>
:host {
width: 100%;
height: 100%;
}
#container {
position: relative;
width: 100%;
height: 100%;
/*top:-15px;*/
}
#map {
position:absolute;
top:0;
bottom:0;
width: 100%;
height: 100%;
}
.swipearea {
height: 50px;
width: 100%;
background-color: rgba(0,0,0,0.3);
position: absolute;
bottom: 0;
left: 0;
z-index: 1;
text-align: center;
padding: 15px;
box-sizing: border-box;
color: white;
}
</style>
<template if="{{accessTokenRoute}}">
<core-ajax auto
id="token"
url="{{accessTokenRoute}}"
handleAs="text"
on-core-response="{{_receiveToken}}">
</core-ajax>
</template>
<div id="container">
<div id='map'></div>
<template if="{{showSwipeBar}}">
<div class="swipearea submainpoint"> Swipe next here </div>
</template>
</div>
</template>
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.js'></script>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-locatecontrol/v0.24.0/L.Control.Locate.js'></script>
<script>
Polymer({
mapId: 'examples.map-i87786ca',
layers: [],
startingZoom: 12,
geolocation: false,
geolocateZoom: 10,
showSwipeBar: false,
observe: {
markerColor: "addMarkers",
startingZoom: "reset",
startingLat: "reset",
startingLng: "reset"
},
ready: function(){
var self = this;
if (self.accessToken){
self._createMap();
} else {
self.accessToken="pk.eyJ1IjoidGhlbG1hIiwiYSI6Ikd3RUlsTTQifQ.i6bojdSBKd6JHENLKsRjTA";
self._createMap();
}
},
_receiveToken: function(event, r){
var self = this;
if (event.detail.response){
L.mapbox.accessToken = self.accessToken || event.detail.response; // Assign the access token
self._createMap();
}
},
_createMap: function(){
var self = this,
mapContainer = self.$.map;
// Assign access token
L.mapbox.accessToken = L.mapbox.accessToken || self.accessToken;
// Create the map inside the div, given the mapId, default lat/long and zoom
self.map = L.mapbox.map(mapContainer, self.mapId);
// If starting coordinates given, set view at that point (setTimeout needed otherwise there are tiling issues)
self.map.on('ready', function(){
if (self.startingLng && self.startingLat){
self.map.setView([self.startingLat, self.startingLng], self.startingZoom);
}
})
// Remove mapbox attribution/branding
mapContainer.querySelector('.leaflet-control-attribution').remove();
// Add layers to map
for (var i=0; i<self.layers.length; i++){
if(self.layers[i] && self.layers[i].layerId){
L.mapbox.tileLayer(self.layers[i].layerId).addTo(self.map);
}
}
// Add button for geolocation
self.geolocate = L.control.locate({ locateOptions: { maxZoom: self.geolocateZoom }}).addTo(self.map);
self.resize();
},
addMarkers: function(){
if (this.input && this.input.length){
var self = this,
markerColor = self.markerColor || window.CoreStyle.g.theme.accent2,
geojson = self.input.map(function(place){
return {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [place.longitude,place.latitude]
},
"properties": {
"title": place.label,
// "description": "1714 14th St NW, Washington DC",
"marker-color": markerColor,
"marker-size": "small"
// "marker-symbol": "monument"
}
}
});
var markers = self.map.featureLayer.setGeoJSON(geojson);
self.map.fitBounds(markers.getBounds());
}
},
accessTokenChanged: function(){
var self = this;
if(L.mapbox.accessToken != self.accessToken){
L.mapbox.accessToken = self.accessToken;
self._createMap();
}
},
animate: function(){
var self = this;
if(self.input && self.input.length){
self.addMarkers();
}
if (self.geolocation && self.map){
setTimeout(function(){
self.geolocate.locate();
},2000)
}
},
reset: function(){
var self = this;
if (self.startingLng && self.startingLng){
self.map.setView([self.startingLat,self.startingLng], self.startingZoom);
}
},
inputChanged: function(){
var self = this;
self.addMarkers();
},
resize: function() {
this.map.invalidateSize();
},
getMetaData: function(){
return {
"name": "lens-viz-mapbox",
"description": "Wrapper for MapBox",
"category":"map",
"version": "0.0.1",
"thumbnail":"mapbox.jpg",
"tags":["map", "mapbox"],
"inputAttr": {
"mapId":{"friendly":"Map ID", "type":"string", "default":""},
"markerColor":{"friendly":"Marker Color", "type":"color", "default":""},
"startingZoom":{"friendly":"Starting Zoom", "type":"range", "default":12, "range":[1,20]},
"startingLat":{"friendly":"Starting Latitude", "type":"float", "default":38.8922},
"startingLng":{"friendly":"Starting Longitude", "type":"float", "default":-77.0348},
"geolocation":{"friendly":"Geolocation?", "type":"boolean", "default": false},
"geolocateZoom":{"friendly":"Geolocate Zoom", "conditionalAttr":"geolocation", "type":"range", "default":10, "range":[1,20]}
}
}
}
});
</script>
</polymer-element>