forked from CrunchyData/pg_tileserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapbox-gl-js-tiles.html
167 lines (148 loc) · 4.4 KB
/
mapbox-gl-js-tiles.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
<html>
<head>
<meta charset='utf-8' />
<title>Vector Tiles in MapBox GL JS</title>
<!-- CSS/JS for Mapbox map -->
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.0/mapbox-gl.css' rel='stylesheet' />
<style>
body {
padding: 0;
margin: 0;
}
html, body, #map {
font-family: sans-serif;
height: 100%;
width: 100%;
}
#meta {
background-color: rgba(255,255,255,0.75);
color: black;
z-index: 2;
position: absolute;
top: 10px;
left: 20px;
padding: 10px 20px;
margin: 0;
}
</style>
</head>
<body>
<div id="meta">
<h2>Mapbox GL JS Tile Map</h2>
<ul>
<li><a href="https://docs.mapbox.com/mapbox-gl-js/api/">Mapbox GL JS</a></li>
</ul>
</div>
<div id="map"></div>
<script>
// Basic map configuration, and a raster base map
// layer from wikimedia. Can add stores/layers at run-time
// or here at configuration time.
var mapConfig = {
'container': 'map',
'center': [0,0],
'zoom': 1,
'hash': true,
'style': {
'version': 8,
'sources': {
'wikimedia': {
'type': 'raster',
'tiles': [
"https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png"
]
}
},
'layers': [{
'type': 'raster',
'id': 'wikimedia-layer',
'source': 'wikimedia',
'minzoom': 0,
'maxzoom': 22
}]
}
};
var map = new mapboxgl.Map(mapConfig);
// Zoom control
map.addControl(new mapboxgl.NavigationControl());
map.addControl(new mapboxgl.AttributionControl({
compact: true,
customAttribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, made with Natural Earth'
}));
// Add the tile layer to the map
// https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip
var vectorSource = "ne-source";
var vectorId = "ne-layer";
// Build the tile URL
var vectorServer = "http://localhost:7800/";
var vectorSourceLayer = "public.ne_50m_admin_0_countries";
// The data table has a lot of columns, we retrieve just three
var vectorProps = "?properties=name,type,pop_est"
var vectorUrl = vectorServer + vectorSourceLayer + "/{z}/{x}/{y}.pbf" + vectorProps;
map.on("load", function() {
// Layers read from sources
map.addSource(vectorSource, {
"type": "vector",
"tiles": [vectorUrl],
"minzoom": 0,
"maxzoom": 22
});
// To get wide rendered boundaries we
// need two layers, one for the boundaries
// and one for the fill
var vectorLayerColor = "blue";
var vectorLayerOutline = {
"id": vectorId + "-outline",
"source": vectorSource,
"source-layer": vectorSourceLayer,
"type": "line",
"paint": {
"line-width": 1.5,
"line-color": vectorLayerColor
}
};
map.addLayer(vectorLayerOutline);
// The fill layer has a unique id and
// we can tie the click action to it below
var vectorLayerFill = {
"id": vectorId,
"source": vectorSource,
"source-layer": vectorSourceLayer,
"type": "fill",
"paint": {
"fill-color": vectorLayerColor,
"fill-opacity": 0.1,
"fill-outline-color": vectorLayerColor
}
};
map.addLayer(vectorLayerFill);
// Utility ot convert feature properties into html
function featureHtml(f) {
var h = "";
var p = f.properties;
for (var k in p) {
h += "<b>" + k + ":</b> " + p[k] + "<br/>";
}
return "<p>" + h + "</p>";
};
// When a click event occurs on a feature in the layer, open a popup at the
// location of the click, with description HTML from its properties.
map.on('click', vectorId, function(e) {
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML(featureHtml(e.features[0]))
.addTo(map);
});
// Change the cursor to a pointer when the mouse is over the fil layer.
map.on('mouseenter', vectorId, function() {
map.getCanvas().style.cursor = 'pointer';
});
// Change it back to a pointer when it leaves.
map.on('mouseleave', vectorId, function() {
map.getCanvas().style.cursor = '';
});
});
</script>
</body>
</html>