-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmapbox-gl-marker.html
284 lines (259 loc) · 7.19 KB
/
mapbox-gl-marker.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
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
<link rel="import" href="../polymer/polymer-element.html">
<!--
The `mapbox-gl-marker` element represents a map marker. It is used as a
child of `mapbox-gl`.
The default anchor is the CENTER of the element.
You can either style the element via CSS selector "div.mapbox-gl-marker", or attached other elements as its
child as shown in the example below.
<b>Example</b>:
```html
<mapbox-gl id="map"
interactive
map="{{map}}"
map-style="mapbox://styles/mapbox/dark-v9"
access-token="<MAPBOX_ACCESS_TOKEN>"
latitude=1.3521
longitude=103.8698
zoom=16
pitch=45
bearing=0>
<mapbox-gl-marker
latitude=1.3521
longitude=103.8698
width=64
height=64
border-radius="50%"
background-image="https://placekitten.com/g/64/64">
</mapbox-gl-marker>
<mapbox-gl-marker
latitude=1.3541 longitude=103.8718
offset-top=15>
<div class="textbox">Some text here</div>
<div class="arrow-down"></div>
</mapbox-gl-marker>
</mapbox-gl>
```
See https://www.mapbox.com/mapbox-gl-js/api/#Marker for
more details.
@demo demo/markers.html Plotting map markers
-->
<dom-module id="mapbox-gl-marker">
<template>
<style>
:host {
display: block;
padding: 0;
}
</style>
<slot></slot>
</template>
<script>
class MapboxGlMarker extends Polymer.Element {
static get is() { return 'mapbox-gl-marker'; }
static get properties() {
return {
/**
* Reference to `Mapbox.Popup` instance to display when the marker is
* clicked.
* @type {Mapbox.Popup}
*/
popup: {
type: Object,
value: null
},
/* show a down pointing arrow to indicate the actual position. */
showArrow: {
type: Boolean,
value: false
},
/* color of the arrow */
arrowColor: {
type: String,
value: '#444'
},
/* css border-radius style */
borderRadius: {
type: String
},
height: {
type: Number,
value: 24
},
width: {
type: Number,
value: 24
},
/*
* latitude of the marker
*/
latitude: {
type: Number
},
/*
* longitude of the marker
*/
longitude: {
type: Number
},
/*
* background color for the marker
*/
backgroundColor: {
type: String
},
/*
* url to the image to use for the background of the marker
*/
backgroundImage: {
type: String
},
/*
* POSTIVE values denotes the number of pixels to offset to the RIGHT,
* while NEGATIVE values denotes the number of pixels to offset to the
* LEFT.
*
* Equivalent to CSS left.
*/
offsetLeft: {
type: Number,
value: 0
},
/*
* POSTIVE values denotes the number of pixels to offset DOWNWARD,
* while NEGATIVE values denotes the number of pixels to offset UPWARD.
*
* Equivalent to CSS top.
*/
offsetTop: {
type: Number,
value: 0
},
/*
* `map` object returned from mapbox-gl
*/
map: {
type: Object
},
/*
* 'mapboxgl.Marker' object
*/
marker: {
type: Object,
notify: true,
readonly: true,
computed: '_createMarker(map, _ele, offsetLeft, offsetTop)'
},
_popup: Object,
_ele: {
type: Object,
computed: '_createMarkerElement(map)'
},
_arr: Object,
_attached: Boolean
};
}
static get observers() {
return ['updateLatLng(marker, latitude, longitude)',
'_updateStyle(_ele, width, height, borderRadius, backgroundColor, backgroundImage)',
'_addDownArrow(_ele, showArrow, width, height, arrowColor)',
'setPopup(_popup, marker)']
}
connectedCallback() {
super.connectedCallback();
this._attached = true;
}
disconnectedCallback() {
super.disconnectedCallback();
this._attached = false;
if (this.marker) {
this.marker.remove();
}
}
/*
* Updates the latitude and longitude of the marker.
*/
updateLatLng(marker, lat, lng) {
if (marker) {
marker.setLngLat([lng, lat]);
}
}
/**
* Attach a `Mapbox.Popup` instance or `mapbox-gl-popup` element to this
* marker.
* @param {Mapbox.Popup|HTMLElement} popup
*/
setPopup(popup) {
if (!popup) return;
if (!this.marker) return;
this._popup = popup.is === 'mapbox-gl-popup' ? popup.popup : popup;
return this.marker.setPopup(this._popup);
}
_createMarkerElement() {
var children = this.shadowRoot
.querySelector('slot')
.assignedNodes({flatten:true});
var len = children.length;
var className = ('mapbox-gl-marker '+this.className).trim();
var ele;
if (len == 0) {
ele = document.createElement('div');
ele.className = className;
} else {
// if more than 1 ele, create a wrapper
ele = document.createElement('div');
ele.className = className;
for (var i=0; i<len; ++i) {
ele.appendChild(children[i]);
}
}
return ele;
}
_createMarker(map, ele, offsetLeft, offsetTop) {
if (!map || !ele) return;
/* global mapboxgl */
return new mapboxgl.Marker(ele, {offset: [offsetLeft, offsetTop]})
.setLngLat([this.longitude, this.latitude])
.addTo(map);
}
_updateStyle(ele, width, height, borderRadius, backgroundColor, backgroundImage) {
if (!ele) return;
ele.style.height=height+'px';
ele.style.width=width+'px';
if (borderRadius)
ele.style.borderRadius = borderRadius;
if (backgroundColor)
ele.style.backgroundColor = backgroundColor;
if (backgroundImage) {
ele.style.backgroundImage = `url(${backgroundImage})`;
}
if (this._arr) {
this._arr.style.transform = `translateX(${width/2}px)`;
}
}
_addDownArrow(ele, showArrow, width, height, arrowColor) {
if (!ele) return;
if (!showArrow) {
if (this._arr) {
ele.removeChild(this._arr);
this.offsetTop = 0;
}
return;
}
var arr = this._arr || document.createElement('div');
arr.style.transform = `translateX(${width/2}px)`;
arr.style.marginLeft = '-8px';
arr.style.position = 'absolute';
arr.style.bottom = '-8px';
arr.style.width = '0px';
arr.style.height = '0px';
arr.style.borderLeft = '8px solid transparent';
arr.style.borderRight = '8px solid transparent';
arr.style.borderTop = `8px solid ${arrowColor}`;
this._arr = arr;
this.offsetTop = -(0.5 * height) - 8;
ele.appendChild(arr);
}
}
window.customElements.define(MapboxGlMarker.is, MapboxGlMarker);
</script>
</dom-module>