-
Notifications
You must be signed in to change notification settings - Fork 8
/
mapbox-heatmap-layer.html
168 lines (160 loc) · 4.59 KB
/
mapbox-heatmap-layer.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
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/lib/elements/dom-repeat.html">
<link rel="import" href="./mapbox-layer.html">
<!--
Creates a heatmap using multiple `mapbox-layers`.
More details @ https://www.mapbox.com/mapbox-gl-js/example/heatmap/
<b>Example</b>
```html
<mapbox-gl id="map"
interactive
map="{{map}}"
map-style="mapbox://styles/mapbox/dark-v9"
access-token="<MAPBOX_ACCESS_TOKEN>"
latitude=40.66995747013945
longitude=-103.59179687498357
zoom=3></mapbox-gl>
<mapbox-heatmap-layer
map="[[map]]"
layer-id="heatmap"
source="geojsonsrc"
radius=80
color="rgba(0, 200, 0, 0.3)"
opacity=0.2
levels="[[levels]]"></mapbox-heatmap-layer>
<geojson-source
cluster
cluster-max-zoom=15
cluster-radius=20
map="[[map]]"
source-id="geojsonsrc"
source-data="https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_10m_parks_and_protected_lands_point.geojson"></geojson-source>
```
```javascript
levels = [{"count": 0, "color": "#EEEEEE", "radius": 2, "opacity": 0.5},
{"count": 5, "color": "#2196F3"},
{"count": 8, "color": "#FFC107"},
{"count": 10, "color": "#F44336"}];
```
@demo demo/heatmap.html Heatmap
-->
<dom-module id="mapbox-heatmap-layer">
<template>
<template is="dom-repeat" items="[[_layers]]">
<mapbox-layer
layer-id="[[item.layerId]]"
map="[[map]]"
source="[[source]]"
source-layer="[[sourceLayer]]"
rendering-type="circle"
radius="[[item.radius]]"
color="[[item.color]]"
opacity="[[item.opacity]]"
blur="[[item.blur]]"
filter="[[item.filter]]"></mapbox-layer>
</template>
</template>
<script>
class MapboxHeatmapLayer extends Polymer.Element {
static get is() { return 'mapbox-heatmap-layer'; }
static get properties() {
return {
/*
* The (map)[https://www.mapbox.com/mapbox-gl-js/api/#Map] instance
* returned by mapboxgl-js
*/
map: {
type: Object
},
/*
* Unique layer name. (Required)
*/
layerId: {
type: String
},
/*
* Name of a source description to be used for this layer.
*/
source: {
type: String
},
/*
* Layer to use from a vector tile source. Required if the source
* supports multiple layers.
*/
sourceLayer: {
type: String
},
/*
* default radius of all the levels
*/
radius: {
type: Number,
value: 70
},
/*
* default opacity of all the levels
*/
opacity: {
type: Number,
value: 1
},
/*
* default color of all the levels.
*/
color: {
type: String,
value: '#4CAF50'
},
/*
* Array of Object describing the levels of the heatmap.
* Each level can have the following fields:
* - count: level should have less than this number of points
* - radius: radius of the cluster for this level
* - color: color of the level
* - opacity: opacity of the level
*/
levels: {
type: Array
},
_layers: {
type: Array,
computed: '_createLevels(levels, map, layerId, source)'
}
};
}
_createLevels(levels) {
if (!levels || !this.map || !this.layerId || !this.source) return;
var len = levels.length;
var layers = new Array(levels.length);
for (var i = 0; i < len; ++i) {
layers[i] = this._createLevel(levels, i);
}
return layers;
}
_createLevel(levels, i) {
var conf = Object.create(null);
var level = levels[i];
var prevlevel = levels[Math.max(i - 1, 0)];
conf.layerId = this.layerId + '_' + i;
conf.color = level.color || this.color;
conf.radius = level.radius || this.radius;
conf.blur = level.blur || 1;
conf.opacity = level.opacity || this.opacity;
if (level.count === 0) {
conf.filter = ['!=', 'cluster', true];
} else if (i == levels.length - 1) {
conf.filter = ['>', 'point_count', prevlevel.count || 0];
} else {
conf.filter = [
'all',
['>', 'point_count', prevlevel.count || 0],
['<=', 'point_count', level.count || 0]
];
}
return conf;
}
}
window.customElements.define(MapboxHeatmapLayer.is, MapboxHeatmapLayer);
</script>
</dom-module>