Created
October 8, 2015 16:54
-
-
Save klokan/5f7bedff2597e3576bbd to your computer and use it in GitHub Desktop.
OpenLayers 3 / OL3 MapBox Vector Tiles (OSM Bright): http://ahocevar.net/ol3/vectortile/examples/mapbox-vector-tiles.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta http-equiv="X-UA-Compatible" content="chrome=1"> | |
| <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width"> | |
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" type="text/css"> | |
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" type="text/css"> | |
| <link rel="stylesheet" href="./resources/prism/prism.css" type="text/css"> | |
| <link rel="stylesheet" href="../css/ol.css" type="text/css"> | |
| <link rel="stylesheet" href="./resources/layout.css" type="text/css"> | |
| <link rel="stylesheet" href="mapbox-vector-tiles.css"> | |
| <script src="./resources/zeroclipboard/ZeroClipboard.min.js"></script> | |
| <title>Mapbox vector tiles example</title> | |
| </head> | |
| <body> | |
| <header class="navbar" role="navigation"> | |
| <div class="container" id="navbar-inner-container"> | |
| <a class="navbar-brand" href="./"><img src="./resources/logo-70x70.png"> OpenLayers 3 Examples</a> | |
| </div> | |
| </header> | |
| <div class="container-fluid"> | |
| <div class="row-fluid"> | |
| <div class="span12"> | |
| <div id="map" class="map"></div> | |
| </div> | |
| </div> | |
| <div class="row-fluid"> | |
| <div class="span12"> | |
| <h4 id="title">Mapbox vector tiles example</h4> | |
| <p id="shortdesc">Example of a Mapbox vector tiles map.</p> | |
| <div id="docs"><p>A vector tiles map which reuses the same tiles for subsequent zoom levels to save bandwith on mobile devices.</p> | |
| </div> | |
| <div id="tags">simple, mapbox, vector, tiles</div> | |
| <div id="api-links">Related API documentation: <ul class="inline"><li><a href="../apidoc/ol.Attribution.html" title="API documentation for ol.Attribution">ol.Attribution</a></li>,<li><a href="../apidoc/ol.Map.html" title="API documentation for ol.Map">ol.Map</a></li>,<li><a href="../apidoc/ol.View.html" title="API documentation for ol.View">ol.View</a></li>,<li><a href="../apidoc/ol.format.MVT.html" title="API documentation for ol.format.MVT">ol.format.MVT</a></li>,<li><a href="../apidoc/ol.layer.VectorTile.html" title="API documentation for ol.layer.VectorTile">ol.layer.VectorTile</a></li>,<li><a href="../apidoc/ol.proj.html" title="API documentation for ol.proj">ol.proj</a></li>,<li><a href="../apidoc/ol.source.VectorTile.html" title="API documentation for ol.source.VectorTile">ol.source.VectorTile</a></li>,<li><a href="../apidoc/ol.style.Fill.html" title="API documentation for ol.style.Fill">ol.style.Fill</a></li>,<li><a href="../apidoc/ol.style.Icon.html" title="API documentation for ol.style.Icon">ol.style.Icon</a></li>,<li><a href="../apidoc/ol.style.Stroke.html" title="API documentation for ol.style.Stroke">ol.style.Stroke</a></li>,<li><a href="../apidoc/ol.style.Style.html" title="API documentation for ol.style.Style">ol.style.Style</a></li>,<li><a href="../apidoc/ol.style.Text.html" title="API documentation for ol.style.Text">ol.style.Text</a></li>,<li><a href="../apidoc/ol.tilegrid.TileGrid.html" title="API documentation for ol.tilegrid.TileGrid">ol.tilegrid.TileGrid</a></li></ul></div> | |
| </div> | |
| </div> | |
| <div class="row-fluid"> | |
| <div id="source-controls"> | |
| <a id="copy-button"><i class="fa fa-clipboard"></i> Copy</a> | |
| <a id="jsfiddle-button"><i class="fa fa-jsfiddle"></i> Edit</a> | |
| </div> | |
| <form method="POST" id="jsfiddle-form" target="_blank" action="http://jsfiddle.net/api/post/jquery/1.11.0/"> | |
| <textarea class="hidden" name="js">// Mapbox access token - request your own at http://mapbox.com | |
| var accessToken = | |
| 'pk.eyJ1IjoiYWhvY2V2YXIiLCJhIjoiRk1kMWZaSSJ9.E5BkluenyWQMsBLsuByrmg'; | |
| // For how many zoom levels do we want to use the same vector tiles? | |
| // 1 means "use tiles from all zoom levels". 2 means "use the same tiles for 2 | |
| // subsequent zoom levels". | |
| var reuseZoomLevels = 2; | |
| // Offset of loaded tiles from web mercator zoom level 0. | |
| // 0 means "At map zoom level 0, use tiles from zoom level 0". 1 means "At map | |
| // zoom level 0, use tiles from zoom level 1". | |
| var zoomOffset = 1; | |
| // Calculation of tile urls | |
| var resolutions = []; | |
| for (var z = zoomOffset / reuseZoomLevels; z <= 22 / reuseZoomLevels; ++z) { | |
| resolutions.push(156543.03392804097 / Math.pow(2, z * reuseZoomLevels)); | |
| } | |
| function tileUrlFunction(tileCoord) { | |
| return ('http://{a-d}.tiles.mapbox.com/v4/mapbox.mapbox-streets-v6/' + | |
| '{z}/{x}/{y}.vector.pbf?access_token=' + accessToken) | |
| .replace('{z}', String(tileCoord[0] * reuseZoomLevels + zoomOffset)) | |
| .replace('{x}', String(tileCoord[1])) | |
| .replace('{y}', String(-tileCoord[2] - 1)) | |
| .replace('{a-d}', 'abcd'.substr( | |
| ((tileCoord[1] << tileCoord[0]) + tileCoord[2]) % 4, 1)); | |
| } | |
| var map = new ol.Map({ | |
| layers: [ | |
| new ol.layer.VectorTile({ | |
| preload: Infinity, | |
| source: new ol.source.VectorTile({ | |
| attributions: [new ol.Attribution({ | |
| html: '© <a href="https://www.mapbox.com/map-feedback/">Mapbox</a> ' + | |
| '© <a href="http://www.openstreetmap.org/copyright">' + | |
| 'OpenStreetMap contributors</a>' | |
| })], | |
| format: new ol.format.MVT(), | |
| tileGrid: new ol.tilegrid.TileGrid({ | |
| extent: ol.proj.get('EPSG:3857').getExtent(), | |
| resolutions: resolutions | |
| }), | |
| tilePixelRatio: 16, | |
| tileUrlFunction: tileUrlFunction | |
| }), | |
| style: (function() { | |
| var fill = new ol.style.Fill({color: ''}); | |
| var stroke = new ol.style.Stroke({color: '', width: 1}); | |
| var polygon = new ol.style.Style({fill: fill}); | |
| var strokedPolygon = new ol.style.Style({fill: fill, stroke: stroke}); | |
| var line = new ol.style.Style({stroke: stroke}); | |
| var text = new ol.style.Style({text: new ol.style.Text({ | |
| text: '', fill: fill, stroke: stroke | |
| })}); | |
| var iconCache = {}; | |
| function getIcon(iconName) { | |
| var icon = iconCache[iconName]; | |
| if (!icon) { | |
| icon = new ol.style.Style({image: new ol.style.Icon({ | |
| src: 'data/maki-icon/' + iconName + '-12.png' | |
| })}); | |
| iconCache[iconName] = icon; | |
| } | |
| return icon; | |
| } | |
| var styles = []; | |
| return function(feature, resolution) { | |
| var length = 0; | |
| var layer = feature.get('layer'); | |
| var cls = feature.get('class'); | |
| var type = feature.get('type'); | |
| var scalerank = feature.get('scalerank'); | |
| var labelrank = feature.get('labelrank'); | |
| var adminLevel = feature.get('admin_level'); | |
| var maritime = feature.get('maritime'); | |
| var disputed = feature.get('disputed'); | |
| var maki = feature.get('maki'); | |
| var geom = feature.getGeometry().getType(); | |
| if (layer == 'landuse' && cls == 'park') { | |
| fill.setColor('#d8e8c8'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'landuse' && cls == 'cemetery') { | |
| fill.setColor('#e0e4dd'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'landuse' && cls == 'hospital') { | |
| fill.setColor('#fde'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'landuse' && cls == 'school') { | |
| fill.setColor('#f0e8f8'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'landuse' && cls == 'wood') { | |
| fill.setColor('rgb(233,238,223)'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'waterway' && | |
| cls != 'river' && cls != 'stream' && cls != 'canal') { | |
| stroke.setColor('#a0c8f0'); | |
| stroke.setWidth(1.3); | |
| styles[length++] = line; | |
| } else if (layer == 'waterway' && cls == 'river') { | |
| stroke.setColor('#a0c8f0'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'waterway' && (cls == 'stream' || | |
| cls == 'canal')) { | |
| stroke.setColor('#a0c8f0'); | |
| stroke.setWidth(1.3); | |
| styles[length++] = line; | |
| } else if (layer == 'water') { | |
| fill.setColor('#a0c8f0'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'aeroway' && geom == 'Polygon') { | |
| fill.setColor('rgb(242,239,235)'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'aeroway' && geom == 'LineString' && | |
| resolution <= 76.43702828517625) { | |
| stroke.setColor('#f0ede9'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'building') { | |
| fill.setColor('#f2eae2'); | |
| stroke.setColor('#dfdbd7'); | |
| stroke.setWidth(1); | |
| styles[length++] = strokedPolygon; | |
| } else if (layer == 'tunnel' && cls == 'motorway_link') { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && cls == 'service') { | |
| stroke.setColor('#cfcdca'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && | |
| (cls == 'street' || cls == 'street_limited')) { | |
| stroke.setColor('#cfcdca'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && cls == 'main' && | |
| resolution <= 1222.99245256282) { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && cls == 'motorway') { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && cls == 'path') { | |
| stroke.setColor('#cba'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && cls == 'major_rail') { | |
| stroke.setColor('#bbb'); | |
| stroke.setWidth(1.4); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && cls == 'motorway_link') { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && (cls == 'street' || | |
| cls == 'street_limited') && geom == 'LineString') { | |
| stroke.setColor('#cfcdca'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && cls == 'main' && | |
| resolution <= 1222.99245256282) { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && cls == 'motorway' && | |
| resolution <= 4891.96981025128) { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && cls == 'path') { | |
| stroke.setColor('#cba'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && cls == 'major_rail') { | |
| stroke.setColor('#bbb'); | |
| stroke.setWidth(1.4); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'motorway_link') { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'motorway') { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'service') { | |
| stroke.setColor('#cfcdca'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && | |
| (cls == 'street' || cls == 'street_limited')) { | |
| stroke.setColor('#cfcdca'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'main' && | |
| resolution <= 1222.99245256282) { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'path') { | |
| stroke.setColor('#cba'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'major_rail') { | |
| stroke.setColor('#bbb'); | |
| stroke.setWidth(1.4); | |
| styles[length++] = line; | |
| } else if (layer == 'admin' && adminLevel >= 3 && maritime === 0) { | |
| stroke.setColor('#9e9cab'); | |
| stroke.setWidth(1); | |
| styles[length++] = line; | |
| } else if (layer == 'admin' && adminLevel == 2 && | |
| disputed === 0 && maritime === 0) { | |
| stroke.setColor('#9e9cab'); | |
| stroke.setWidth(1); | |
| styles[length++] = line; | |
| } else if (layer == 'admin' && adminLevel == 2 && | |
| disputed === 1 && maritime === 0) { | |
| stroke.setColor('#9e9cab'); | |
| stroke.setWidth(1); | |
| styles[length++] = line; | |
| } else if (layer == 'admin' && adminLevel >= 3 && maritime === 1) { | |
| stroke.setColor('#a0c8f0'); | |
| stroke.setWidth(1); | |
| styles[length++] = line; | |
| } else if (layer == 'admin' && adminLevel == 2 && maritime === 1) { | |
| stroke.setColor('#a0c8f0'); | |
| stroke.setWidth(1); | |
| styles[length++] = line; | |
| } else if (layer == 'country_label' && scalerank === 1) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('bold 11px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#334'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(2); | |
| styles[length++] = text; | |
| } else if (layer == 'country_label' && scalerank === 2 && | |
| resolution <= 19567.87924100512) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('bold 10px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#334'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(2); | |
| styles[length++] = text; | |
| } else if (layer == 'country_label' && scalerank === 3 && | |
| resolution <= 9783.93962050256) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('bold 9px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#334'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(2); | |
| styles[length++] = text; | |
| } else if (layer == 'country_label' && scalerank === 4 && | |
| resolution <= 4891.96981025128) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('bold 8px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#334'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(2); | |
| styles[length++] = text; | |
| } else if (layer == 'marine_label' && labelrank === 1 && | |
| geom == 'Point') { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont( | |
| 'italic 11px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#74aee9'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(0.75); | |
| styles[length++] = text; | |
| } else if (layer == 'marine_label' && labelrank === 2 && | |
| geom == 'Point') { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont( | |
| 'italic 11px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#74aee9'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(0.75); | |
| styles[length++] = text; | |
| } else if (layer == 'marine_label' && labelrank === 3 && | |
| geom == 'Point') { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont( | |
| 'italic 10px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#74aee9'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(0.75); | |
| styles[length++] = text; | |
| } else if (layer == 'marine_label' && labelrank === 4 && | |
| geom == 'Point') { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont( | |
| 'italic 9px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#74aee9'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(0.75); | |
| styles[length++] = text; | |
| } else if (layer == 'place_label' && type == 'city' && | |
| resolution <= 1222.99245256282) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('11px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#333'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = text; | |
| } else if (layer == 'place_label' && type == 'town' && | |
| resolution <= 305.748113140705) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('9px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#333'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = text; | |
| } else if (layer == 'place_label' && type == 'village' && | |
| resolution <= 38.21851414258813) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('8px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#333'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = text; | |
| } else if (layer == 'place_label' && | |
| resolution <= 19.109257071294063 && (type == 'hamlet' || | |
| type == 'suburb' || type == 'neighbourhood')) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('bold 9px "Arial Narrow"'); | |
| fill.setColor('#633'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = text; | |
| } else if (layer == 'poi_label' && resolution <= 19.109257071294063 && | |
| scalerank == 1 && maki !== 'marker') { | |
| styles[length++] = getIcon(maki); | |
| } else if (layer == 'poi_label' && resolution <= 9.554628535647032 && | |
| scalerank == 2 && maki !== 'marker') { | |
| styles[length++] = getIcon(maki); | |
| } else if (layer == 'poi_label' && resolution <= 4.777314267823516 && | |
| scalerank == 3 && maki !== 'marker') { | |
| styles[length++] = getIcon(maki); | |
| } else if (layer == 'poi_label' && resolution <= 2.388657133911758 && | |
| scalerank == 4 && maki !== 'marker') { | |
| styles[length++] = getIcon(maki); | |
| } else if (layer == 'poi_label' && resolution <= 1.194328566955879 && | |
| scalerank >= 5 && maki !== 'marker') { | |
| styles[length++] = getIcon(maki); | |
| } | |
| styles.length = length; | |
| return styles; | |
| }; | |
| })() | |
| }) | |
| ], | |
| target: 'map', | |
| view: new ol.View({ | |
| center: [0, 0], | |
| minZoom: 1, | |
| zoom: 2 | |
| }) | |
| }); | |
| </textarea> | |
| <textarea class="hidden" name="css">.map { | |
| background: #f8f4f0; | |
| } | |
| </textarea> | |
| <textarea class="hidden" name="html"><div class="row-fluid"> | |
| <div class="span12"> | |
| <div id="map" class="map"></div> | |
| </div> | |
| </div> | |
| </textarea> | |
| <input type="hidden" name="wrap" value="l"> | |
| <input type="hidden" name="resources" value="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css,https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js,http://openlayers.org/en/v3.9.0/css/ol.css,http://openlayers.org/en/v3.9.0/build/ol.js"> | |
| </form> | |
| <pre><code id="example-source" class="language-markup"><!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Mapbox vector tiles example</title> | |
| <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script> | |
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> | |
| <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> | |
| <link rel="stylesheet" href="http://openlayers.org/en/v3.9.0/css/ol.css" type="text/css"> | |
| <script src="http://openlayers.org/en/v3.9.0/build/ol.js"></script> | |
| <style> | |
| .map { | |
| background: #f8f4f0; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container-fluid"> | |
| <div class="row-fluid"> | |
| <div class="span12"> | |
| <div id="map" class="map"></div> | |
| </div> | |
| </div> | |
| </div> | |
| <script> | |
| // Mapbox access token - request your own at http://mapbox.com | |
| var accessToken = | |
| 'pk.eyJ1IjoiYWhvY2V2YXIiLCJhIjoiRk1kMWZaSSJ9.E5BkluenyWQMsBLsuByrmg'; | |
| // For how many zoom levels do we want to use the same vector tiles? | |
| // 1 means "use tiles from all zoom levels". 2 means "use the same tiles for 2 | |
| // subsequent zoom levels". | |
| var reuseZoomLevels = 2; | |
| // Offset of loaded tiles from web mercator zoom level 0. | |
| // 0 means "At map zoom level 0, use tiles from zoom level 0". 1 means "At map | |
| // zoom level 0, use tiles from zoom level 1". | |
| var zoomOffset = 1; | |
| // Calculation of tile urls | |
| var resolutions = []; | |
| for (var z = zoomOffset / reuseZoomLevels; z <= 22 / reuseZoomLevels; ++z) { | |
| resolutions.push(156543.03392804097 / Math.pow(2, z * reuseZoomLevels)); | |
| } | |
| function tileUrlFunction(tileCoord) { | |
| return ('http://{a-d}.tiles.mapbox.com/v4/mapbox.mapbox-streets-v6/' + | |
| '{z}/{x}/{y}.vector.pbf?access_token=' + accessToken) | |
| .replace('{z}', String(tileCoord[0] * reuseZoomLevels + zoomOffset)) | |
| .replace('{x}', String(tileCoord[1])) | |
| .replace('{y}', String(-tileCoord[2] - 1)) | |
| .replace('{a-d}', 'abcd'.substr( | |
| ((tileCoord[1] << tileCoord[0]) + tileCoord[2]) % 4, 1)); | |
| } | |
| var map = new ol.Map({ | |
| layers: [ | |
| new ol.layer.VectorTile({ | |
| preload: Infinity, | |
| source: new ol.source.VectorTile({ | |
| attributions: [new ol.Attribution({ | |
| html: '© <a href="https://www.mapbox.com/map-feedback/">Mapbox</a> ' + | |
| '© <a href="http://www.openstreetmap.org/copyright">' + | |
| 'OpenStreetMap contributors</a>' | |
| })], | |
| format: new ol.format.MVT(), | |
| tileGrid: new ol.tilegrid.TileGrid({ | |
| extent: ol.proj.get('EPSG:3857').getExtent(), | |
| resolutions: resolutions | |
| }), | |
| tilePixelRatio: 16, | |
| tileUrlFunction: tileUrlFunction | |
| }), | |
| style: (function() { | |
| var fill = new ol.style.Fill({color: ''}); | |
| var stroke = new ol.style.Stroke({color: '', width: 1}); | |
| var polygon = new ol.style.Style({fill: fill}); | |
| var strokedPolygon = new ol.style.Style({fill: fill, stroke: stroke}); | |
| var line = new ol.style.Style({stroke: stroke}); | |
| var text = new ol.style.Style({text: new ol.style.Text({ | |
| text: '', fill: fill, stroke: stroke | |
| })}); | |
| var iconCache = {}; | |
| function getIcon(iconName) { | |
| var icon = iconCache[iconName]; | |
| if (!icon) { | |
| icon = new ol.style.Style({image: new ol.style.Icon({ | |
| src: 'data/maki-icon/' + iconName + '-12.png' | |
| })}); | |
| iconCache[iconName] = icon; | |
| } | |
| return icon; | |
| } | |
| var styles = []; | |
| return function(feature, resolution) { | |
| var length = 0; | |
| var layer = feature.get('layer'); | |
| var cls = feature.get('class'); | |
| var type = feature.get('type'); | |
| var scalerank = feature.get('scalerank'); | |
| var labelrank = feature.get('labelrank'); | |
| var adminLevel = feature.get('admin_level'); | |
| var maritime = feature.get('maritime'); | |
| var disputed = feature.get('disputed'); | |
| var maki = feature.get('maki'); | |
| var geom = feature.getGeometry().getType(); | |
| if (layer == 'landuse' && cls == 'park') { | |
| fill.setColor('#d8e8c8'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'landuse' && cls == 'cemetery') { | |
| fill.setColor('#e0e4dd'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'landuse' && cls == 'hospital') { | |
| fill.setColor('#fde'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'landuse' && cls == 'school') { | |
| fill.setColor('#f0e8f8'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'landuse' && cls == 'wood') { | |
| fill.setColor('rgb(233,238,223)'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'waterway' && | |
| cls != 'river' && cls != 'stream' && cls != 'canal') { | |
| stroke.setColor('#a0c8f0'); | |
| stroke.setWidth(1.3); | |
| styles[length++] = line; | |
| } else if (layer == 'waterway' && cls == 'river') { | |
| stroke.setColor('#a0c8f0'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'waterway' && (cls == 'stream' || | |
| cls == 'canal')) { | |
| stroke.setColor('#a0c8f0'); | |
| stroke.setWidth(1.3); | |
| styles[length++] = line; | |
| } else if (layer == 'water') { | |
| fill.setColor('#a0c8f0'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'aeroway' && geom == 'Polygon') { | |
| fill.setColor('rgb(242,239,235)'); | |
| styles[length++] = polygon; | |
| } else if (layer == 'aeroway' && geom == 'LineString' && | |
| resolution <= 76.43702828517625) { | |
| stroke.setColor('#f0ede9'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'building') { | |
| fill.setColor('#f2eae2'); | |
| stroke.setColor('#dfdbd7'); | |
| stroke.setWidth(1); | |
| styles[length++] = strokedPolygon; | |
| } else if (layer == 'tunnel' && cls == 'motorway_link') { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && cls == 'service') { | |
| stroke.setColor('#cfcdca'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && | |
| (cls == 'street' || cls == 'street_limited')) { | |
| stroke.setColor('#cfcdca'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && cls == 'main' && | |
| resolution <= 1222.99245256282) { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && cls == 'motorway') { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && cls == 'path') { | |
| stroke.setColor('#cba'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'tunnel' && cls == 'major_rail') { | |
| stroke.setColor('#bbb'); | |
| stroke.setWidth(1.4); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && cls == 'motorway_link') { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && (cls == 'street' || | |
| cls == 'street_limited') && geom == 'LineString') { | |
| stroke.setColor('#cfcdca'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && cls == 'main' && | |
| resolution <= 1222.99245256282) { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && cls == 'motorway' && | |
| resolution <= 4891.96981025128) { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && cls == 'path') { | |
| stroke.setColor('#cba'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'road' && cls == 'major_rail') { | |
| stroke.setColor('#bbb'); | |
| stroke.setWidth(1.4); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'motorway_link') { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'motorway') { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'service') { | |
| stroke.setColor('#cfcdca'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && | |
| (cls == 'street' || cls == 'street_limited')) { | |
| stroke.setColor('#cfcdca'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'main' && | |
| resolution <= 1222.99245256282) { | |
| stroke.setColor('#e9ac77'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'path') { | |
| stroke.setColor('#cba'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = line; | |
| } else if (layer == 'bridge' && cls == 'major_rail') { | |
| stroke.setColor('#bbb'); | |
| stroke.setWidth(1.4); | |
| styles[length++] = line; | |
| } else if (layer == 'admin' && adminLevel >= 3 && maritime === 0) { | |
| stroke.setColor('#9e9cab'); | |
| stroke.setWidth(1); | |
| styles[length++] = line; | |
| } else if (layer == 'admin' && adminLevel == 2 && | |
| disputed === 0 && maritime === 0) { | |
| stroke.setColor('#9e9cab'); | |
| stroke.setWidth(1); | |
| styles[length++] = line; | |
| } else if (layer == 'admin' && adminLevel == 2 && | |
| disputed === 1 && maritime === 0) { | |
| stroke.setColor('#9e9cab'); | |
| stroke.setWidth(1); | |
| styles[length++] = line; | |
| } else if (layer == 'admin' && adminLevel >= 3 && maritime === 1) { | |
| stroke.setColor('#a0c8f0'); | |
| stroke.setWidth(1); | |
| styles[length++] = line; | |
| } else if (layer == 'admin' && adminLevel == 2 && maritime === 1) { | |
| stroke.setColor('#a0c8f0'); | |
| stroke.setWidth(1); | |
| styles[length++] = line; | |
| } else if (layer == 'country_label' && scalerank === 1) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('bold 11px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#334'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(2); | |
| styles[length++] = text; | |
| } else if (layer == 'country_label' && scalerank === 2 && | |
| resolution <= 19567.87924100512) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('bold 10px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#334'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(2); | |
| styles[length++] = text; | |
| } else if (layer == 'country_label' && scalerank === 3 && | |
| resolution <= 9783.93962050256) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('bold 9px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#334'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(2); | |
| styles[length++] = text; | |
| } else if (layer == 'country_label' && scalerank === 4 && | |
| resolution <= 4891.96981025128) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('bold 8px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#334'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(2); | |
| styles[length++] = text; | |
| } else if (layer == 'marine_label' && labelrank === 1 && | |
| geom == 'Point') { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont( | |
| 'italic 11px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#74aee9'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(0.75); | |
| styles[length++] = text; | |
| } else if (layer == 'marine_label' && labelrank === 2 && | |
| geom == 'Point') { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont( | |
| 'italic 11px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#74aee9'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(0.75); | |
| styles[length++] = text; | |
| } else if (layer == 'marine_label' && labelrank === 3 && | |
| geom == 'Point') { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont( | |
| 'italic 10px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#74aee9'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(0.75); | |
| styles[length++] = text; | |
| } else if (layer == 'marine_label' && labelrank === 4 && | |
| geom == 'Point') { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont( | |
| 'italic 9px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#74aee9'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(0.75); | |
| styles[length++] = text; | |
| } else if (layer == 'place_label' && type == 'city' && | |
| resolution <= 1222.99245256282) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('11px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#333'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = text; | |
| } else if (layer == 'place_label' && type == 'town' && | |
| resolution <= 305.748113140705) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('9px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#333'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = text; | |
| } else if (layer == 'place_label' && type == 'village' && | |
| resolution <= 38.21851414258813) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('8px "Open Sans", "Arial Unicode MS"'); | |
| fill.setColor('#333'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = text; | |
| } else if (layer == 'place_label' && | |
| resolution <= 19.109257071294063 && (type == 'hamlet' || | |
| type == 'suburb' || type == 'neighbourhood')) { | |
| text.getText().setText(feature.get('name_en')); | |
| text.getText().setFont('bold 9px "Arial Narrow"'); | |
| fill.setColor('#633'); | |
| stroke.setColor('rgba(255,255,255,0.8)'); | |
| stroke.setWidth(1.2); | |
| styles[length++] = text; | |
| } else if (layer == 'poi_label' && resolution <= 19.109257071294063 && | |
| scalerank == 1 && maki !== 'marker') { | |
| styles[length++] = getIcon(maki); | |
| } else if (layer == 'poi_label' && resolution <= 9.554628535647032 && | |
| scalerank == 2 && maki !== 'marker') { | |
| styles[length++] = getIcon(maki); | |
| } else if (layer == 'poi_label' && resolution <= 4.777314267823516 && | |
| scalerank == 3 && maki !== 'marker') { | |
| styles[length++] = getIcon(maki); | |
| } else if (layer == 'poi_label' && resolution <= 2.388657133911758 && | |
| scalerank == 4 && maki !== 'marker') { | |
| styles[length++] = getIcon(maki); | |
| } else if (layer == 'poi_label' && resolution <= 1.194328566955879 && | |
| scalerank >= 5 && maki !== 'marker') { | |
| styles[length++] = getIcon(maki); | |
| } | |
| styles.length = length; | |
| return styles; | |
| }; | |
| })() | |
| }) | |
| ], | |
| target: 'map', | |
| view: new ol.View({ | |
| center: [0, 0], | |
| minZoom: 1, | |
| zoom: 2 | |
| }) | |
| }); | |
| </script> | |
| </body> | |
| </html></code></pre> | |
| </div> | |
| </div> | |
| <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script> | |
| <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> | |
| <script src="./resources/common.js"></script> | |
| <script src="./resources/prism/prism.min.js"></script> | |
| <script src="loader.js?id=mapbox-vector-tiles"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment