UNPKG

1.94 kBJavaScriptView Raw
1goog.require('ol.Map');
2goog.require('ol.View');
3goog.require('ol.format.MVT');
4goog.require('ol.layer.VectorTile');
5goog.require('ol.proj');
6goog.require('ol.source.VectorTile');
7goog.require('ol.style.Fill');
8goog.require('ol.style.Icon');
9goog.require('ol.style.Stroke');
10goog.require('ol.style.Style');
11goog.require('ol.style.Text');
12goog.require('ol.tilegrid.TileGrid');
13
14
15var key = 'pk.eyJ1IjoiYWhvY2V2YXIiLCJhIjoiRk1kMWZaSSJ9.E5BkluenyWQMsBLsuByrmg';
16
17// Calculation of resolutions that match zoom levels 1, 3, 5, 7, 9, 11, 13, 15.
18var resolutions = [];
19for (var i = 0; i <= 8; ++i) {
20 resolutions.push(156543.03392804097 / Math.pow(2, i * 2));
21}
22// Calculation of tile urls for zoom levels 1, 3, 5, 7, 9, 11, 13, 15.
23function tileUrlFunction(tileCoord) {
24 return ('https://{a-d}.tiles.mapbox.com/v4/mapbox.mapbox-streets-v6/' +
25 '{z}/{x}/{y}.vector.pbf?access_token=' + key)
26 .replace('{z}', String(tileCoord[0] * 2 - 1))
27 .replace('{x}', String(tileCoord[1]))
28 .replace('{y}', String(-tileCoord[2] - 1))
29 .replace('{a-d}', 'abcd'.substr(
30 ((tileCoord[1] << tileCoord[0]) + tileCoord[2]) % 4, 1));
31}
32
33var map = new ol.Map({
34 layers: [
35 new ol.layer.VectorTile({
36 source: new ol.source.VectorTile({
37 attributions: '© <a href="https://www.mapbox.com/map-feedback/">Mapbox</a> ' +
38 '© <a href="https://www.openstreetmap.org/copyright">' +
39 'OpenStreetMap contributors</a>',
40 format: new ol.format.MVT(),
41 tileGrid: new ol.tilegrid.TileGrid({
42 extent: ol.proj.get('EPSG:3857').getExtent(),
43 resolutions: resolutions,
44 tileSize: 512
45 }),
46 tileUrlFunction: tileUrlFunction
47 }),
48 style: createMapboxStreetsV6Style(ol.style.Style, ol.style.Fill, ol.style.Stroke, ol.style.Icon, ol.style.Text)
49 })
50 ],
51 target: 'map',
52 view: new ol.View({
53 center: [0, 0],
54 minZoom: 1,
55 zoom: 2
56 })
57});