|
| 1 | +<!DOCTYPE html> |
| 2 | +<!-- |
| 3 | +
|
| 4 | +This example displays a map using Tiles and Styles generated locally. |
| 5 | +Use it to preview basemap changes made in this repository. |
| 6 | +
|
| 7 | +Start by building tiles/data/monaco.pmtiles: |
| 8 | +
|
| 9 | + cd tiles |
| 10 | + mvn clean package -DskipTests |
| 11 | + java -jar target/*-with-deps.jar --download --area=monaco --force |
| 12 | + cd - |
| 13 | +
|
| 14 | +Generate styles/style.json with sprites and fonts in a local basemaps-assets directory: |
| 15 | +
|
| 16 | + cd styles |
| 17 | + npm run generate_style style.json https://example.com/tilejson.json light en \ |
| 18 | + 'http://localhost:8080/basemaps-assets/sprites/v4/light' \ |
| 19 | + 'http://localhost:8080/basemaps-assets/fonts/{fontstack}/{range}.pbf' |
| 20 | + cd - |
| 21 | +
|
| 22 | +Populate basemaps-assets directory with fonts and sprite sheets: |
| 23 | +
|
| 24 | + curl -L https://github.com/protomaps/basemaps-assets/archive/refs/heads/main.zip -o /tmp/basemaps-assets-main.zip |
| 25 | + unzip /tmp/basemaps-assets-main.zip 'basemaps-assets-main/fonts/*' 'basemaps-assets-main/sprites/*' -d /tmp |
| 26 | + mv /tmp/basemaps-assets-main basemaps-assets |
| 27 | +
|
| 28 | +Run local web server to view http://localhost:8080/examples/local.html: |
| 29 | +
|
| 30 | + npx http-server -p 8080 -c-1 |
| 31 | +
|
| 32 | +--> |
| 33 | +<html lang="en"> |
| 34 | +<head> |
| 35 | + <meta charset="UTF-8"> |
| 36 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 37 | + <title>Local Styles and Tiles - Protomaps Starter</title> |
| 38 | + |
| 39 | + <!-- MapLibre GL JS --> |
| 40 | + <script src="https://unpkg.com/maplibre-gl@5.15.0/dist/maplibre-gl.js"></script> |
| 41 | + <link href="https://unpkg.com/maplibre-gl@5.15.0/dist/maplibre-gl.css" rel="stylesheet"> |
| 42 | + |
| 43 | + <!-- PMTiles protocol --> |
| 44 | + <script src="https://unpkg.com/pmtiles@4.3.0/dist/pmtiles.js"></script> |
| 45 | + |
| 46 | + <style> |
| 47 | + body { |
| 48 | + margin: 0; |
| 49 | + padding: 0; |
| 50 | + } |
| 51 | + #map { |
| 52 | + position: absolute; |
| 53 | + top: 0; |
| 54 | + bottom: 0; |
| 55 | + width: 100%; |
| 56 | + } |
| 57 | + </style> |
| 58 | +</head> |
| 59 | +<body> |
| 60 | + <div id="map"></div> |
| 61 | + |
| 62 | + <script> |
| 63 | + // Register PMTiles protocol |
| 64 | + let protocol = new pmtiles.Protocol(); |
| 65 | + maplibregl.addProtocol("pmtiles", protocol.tile); |
| 66 | + |
| 67 | + // Load and modify the style to use local PMTiles |
| 68 | + fetch('../styles/style.json') |
| 69 | + .then(response => response.json()) |
| 70 | + .then(style => { |
| 71 | + // Replace the source URL with the local PMTiles file |
| 72 | + style.sources.protomaps = { |
| 73 | + type: "vector", |
| 74 | + url: "pmtiles://../tiles/monaco.pmtiles", |
| 75 | + attribution: style.sources.protomaps.attribution |
| 76 | + }; |
| 77 | + |
| 78 | + // Initialize map with modified style |
| 79 | + const map = new maplibregl.Map({ |
| 80 | + container: 'map', |
| 81 | + style: style, |
| 82 | + center: [7.41488, 43.73019], // Monaco [longitude, latitude] |
| 83 | + zoom: 16, |
| 84 | + hash: true // Allows sharing location via URL hash |
| 85 | + }); |
| 86 | + |
| 87 | + // Add navigation controls |
| 88 | + map.addControl(new maplibregl.NavigationControl(), 'top-right'); |
| 89 | + |
| 90 | + // Add scale control |
| 91 | + map.addControl(new maplibregl.ScaleControl({ |
| 92 | + maxWidth: 80, |
| 93 | + unit: 'metric' |
| 94 | + }), 'bottom-left'); |
| 95 | + }) |
| 96 | + .catch(error => { |
| 97 | + console.error('Error loading style:', error); |
| 98 | + }); |
| 99 | + </script> |
| 100 | +</body> |
| 101 | +</html> |
0 commit comments