Display vector tiles¶
Display vector tiles
display_vector_tiles.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Display vector tiles</title>
<meta property="og:description" content="Display vector tiles" />
<script src="https://resource.mapray.com/mapray-js/v0.9.6/mapray.min.js"></script>
<script src="https://resource.mapray.com/ui/v0.9.6/maprayui.min.js"></script>
<link rel="stylesheet" href="https://resource.mapray.com/styles/v1/mapray.css">
<style>
body {margin: 0; padding: 0;}
html, body, div#mapray-container { height: 100%; }
</style>
</head>
<body>
<div id="mapray-container"></div>
</body>
</html>
<script>
var style_json = {
"version": 8,
"sources": {
"ofmtiles": {
"type": "vector",
"minzoom": 2,
"maxzoom": 14,
"tiles": [ "https://tiles.openfreemap.org/planet/20241016_001001_pt/{z}/{x}/{y}.pbf" ]
}
},
"sprite_ofm": "https://tiles.openfreemap.org/sprites/ofm_f384/ofm",
"sprite": "https://opentiles.mapray.com/sprite/maki/sprite",
"glyphs": "https://tiles.openfreemap.org/fonts/{fontstack}/{range}.pbf",
"layers": [
{
"id": "place_town",
"type": "symbol",
"source": "ofmtiles",
"source-layer": "place",
"minzoom": 2,
"filter": [
"match",
[ "get", "class" ],
[ "town" ], true,
false
],
"layout": {
"icon-image": ["image", "town"],
"icon-size": 1.5,
"text-field": ["get", "name"],
"text-font": [ "Noto Sans Italic" ],
"text-letter-spacing": 0.1,
"text-max-width": 9,
"text-size": [
"interpolate",
[ "linear" ],
[ "zoom" ],
8, 9, // zoom 8 text-size 9
12, 10 // zoom 12 text-size 10
],
"text-transform": "uppercase",
"text-anchor": "top",
"text-offset": [ 0, -2 ]
},
"paint": {
"icon-color": "#edf",
"icon-halo-color": [ "rgb", 96, 96, 96],
"icon-halo-width": 1,
"text-color": "#edf",
"text-halo-blur": 1,
"text-halo-color": "#666",
"text-halo-width": 1
}
},
{
"id": "place_city",
"type": "symbol",
"source": "ofmtiles",
"source-layer": "place",
"minzoom": 2,
"filter": [
"match",
[ "get", "class" ],
[ "city" ], true,
[ "suburb" ], true,
false
],
"layout": {
"icon-image": ["image", "city"],
"icon-size": 1.5,
"text-field": ["get", "name"],
"text-font": [ "Noto Sans Italic" ],
"text-letter-spacing": 0.1,
"text-max-width": 9,
"text-size": [
"interpolate",
[ "linear" ],
[ "zoom" ],
8, 9, // zoom 8 text-size 9
12, 10 // zoom 12 text-size 10
],
"text-transform": "uppercase",
"text-anchor": "top",
"text-offset": [ 0, -2 ]
},
"paint": {
"icon-color": [ "match", ["get", "class"], ["suburb"], "#fdd", "#faa"], // city #faa suburb #fdd
"icon-halo-color": [ "rgb", 96, 96, 96],
"icon-halo-width": 1,
"text-color": [ "match", ["get", "class"], ["suburb"], "#fdd", "#faa"], // city #faa suburb #fdd
"text-halo-blur": 1,
"text-halo-color": "#666",
"text-halo-width": 1
}
},
{
"id": "place_prefecture",
"type": "symbol",
"source": "ofmtiles",
"source-layer": "place",
"minzoom": 2,
"filter": [
"match",
[ "get", "class" ],
[ "province" ], true,
false
],
"layout": {
"icon-image": ["image", "circle"],
"icon-size": 1,
"text-field": ["get", "name"],
"text-font": [ "Noto Sans Italic" ],
"text-letter-spacing": 0.1,
"text-max-width": 9,
"text-size": [
"interpolate",
[ "linear" ],
[ "zoom" ],
8, 9, // zoom 8 text-size 9
12, 10 // zoom 12 text-size 10
],
"text-transform": "uppercase",
"text-anchor": "top",
"text-offset": [ 0, -2 ]
},
"paint": {
"icon-color": "#f88",
"icon-halo-color": ["rgb", 64, 64, 64],
"icon-halo-width": 1,
"text-color": "#f88",
"text-halo-blur": 1,
"text-halo-color": "#444",
"text-halo-width": 1
}
},
{
"id": "place_capital",
"type": "symbol",
"source": "ofmtiles",
"source-layer": "place",
"minzoom": 2,
"filter": [
"all",
[ "match",
[ "get", "class" ],
[ "city" ], true,
false ],
[ "match",
[ "get", "capital" ],
2, true,
false ]
],
"layout": {
"icon-image": ["image", "circle"],
"icon-size": 1.5,
"text-field": ["get", "name"],
"text-font": [ "Noto Sans Italic" ],
"text-letter-spacing": 0.1,
"text-max-width": 9,
"text-size": [
"interpolate",
[ "linear" ],
[ "zoom" ],
8, 9, // zoom 8 text-size 9
12, 10 // zoom 12 text-size 10
],
"text-transform": "uppercase",
"text-anchor": "top",
"text-offset": [ 0, -2 ]
},
"paint": {
"icon-color": "#f8c",
"icon-halo-color": [ "rgb", 96, 96, 96],
"icon-halo-width": 1,
"text-color": "#f8c",
"text-halo-blur": 1,
"text-halo-color": "#666",
"text-halo-width": 1
}
},
{
"id": "place_country",
"type": "symbol",
"source": "ofmtiles",
"source-layer": "place",
"minzoom": 2,
"filter": [
"match",
[ "get", "class" ],
[ "country" ], true,
false
],
"layout": {
"icon-image": ["image", "circle"],
"icon-size": 1,
"text-field": [ "get", "name" ],
"text-font": [ "Noto Sans Italic" ],
"text-letter-spacing": 0.1,
"text-max-width": 9,
"text-size": [
"interpolate",
[ "linear" ],
[ "zoom" ],
8, 14, // zoom 8 text-size 14
12, 16 // zoom 12 text-size 16
],
"text-transform": "uppercase",
"text-anchor": "top",
"text-offset": [ 0, -2 ]
},
"paint": {
"icon-color": "#f44",
"icon-halo-color": ["rgb", 0, 0, 0],
"icon-halo-width": 1,
"text-color": "#f44",
"text-halo-blur": 1,
"text-halo-color": "#000",
"text-halo-width": 1
}
}
]
}
class TileJsonProvider extends mapray.vectile.TileProvider {
constructor() {
super();
}
requestMeta() {
const controller = new AbortController();
return {
promise: this.getMetaData(),
canceller: () => { controller.abort(); },
};
}
requestTile( area ) {
const controller = new AbortController();
return {
promise: this.getTileBinary( area ),
canceller: () => { controller.abort(); },
};
}
init( source ) {
if ( source && source['tiles'] ) {
this.prefix = source['tiles'][0];
this.minLevel = ( source?.minzoom ) ?? 0;
this.maxLevel = ( source?.maxzoom ) ?? 22;
}
}
getMetaData() {
return Promise.resolve( {
min_level: Number( this.minLevel ),
max_level: Number( this.maxLevel ),
} );
}
async getTileBinary( area ) {
const url = this.makeTileDataURL( area );
const response = await fetch( url );
if ( response.ok ) {
return response.arrayBuffer();
} else {
throw new Error( response.statusText );
}
}
makeTileDataURL( area ) {
return this.prefix.replace( '{z}', `${area.z}` ).replace( '{x}', `${area.x}` ).replace( '{y}', `${area.y}` );
}
}
class TileJsonProviderFactory extends mapray.vectile.ProviderFactory {
constructor() {
super();
this.provider = new TileJsonProvider();
}
createTileProvider( _sourceId, jsonSource ) {
if ( this.provider ) {
this.provider.init( jsonSource );
}
return this.provider;
}
createSpriteProvider( location ) {
return location ? new mapray.vectile.StandardSpriteProvider( location ) : null;
}
}
// Set up your access token, which can be created in Mapray Cloud.
var apikey= "<YOUR_MAPRAY_API_KEY>";
const uiviewer = new maprayui.StandardUIViewer( "mapray-container", apikey);
uiviewer.setCameraPosition( {
longitude: 140.092591,
latitude: 34.047398,
height: 268191
} );
uiviewer.setLookAtPosition( {
longitude: 140.071835,
latitude: 35.731372,
height: 25
} );
var style = mapray.vectile.StyleManager.create( uiviewer.viewer, style_json, new TileJsonProviderFactory() );
style.promise
.then( ( manager ) => {
uiviewer.viewer.setVectileManager( manager );
} )
.catch( () => uiviewer.viewer.setVectileManager( null ) );
</script>
Info
このサンプルコードは、<YOUR_MAPRAY_API_KEY>を、あなたのMapray CloudアカウントのAPI Keyに置き換えるまで、期待通りに動作しません。