1
2
3
4
5
6
7
8
9import { MapView } from "mapgpu";
10import { RasterTileLayer, type RasterTileLayerOptions } from "mapgpu/layers";
11import { RenderEngine } from "mapgpu/render";
12
13import type { RunResultObject } from "@/components/examples/ExampleCanvas";
14
15type ProviderId = "osm" | "arcgis" | "carto-dark" | "opentopo";
16
17
18
19const PROVIDERS: Record<ProviderId, RasterTileLayerOptions> = {
20 osm: {
21 id: "osm",
22 urlTemplate: "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
23 maxZoom: 19,
24 attribution: "© OpenStreetMap contributors",
25 },
26 arcgis: {
27 id: "arcgis",
28 urlTemplate:
29 "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}",
30 maxZoom: 19,
31 attribution: "© Esri · DigitalGlobe · GeoEye · Earthstar Geographics",
32 },
33 "carto-dark": {
34 id: "carto-dark",
35 urlTemplate: "https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
36 subdomains: ["a", "b", "c", "d"],
37 maxZoom: 19,
38 attribution: "© CARTO · © OpenStreetMap contributors",
39 },
40 opentopo: {
41 id: "opentopo",
42 urlTemplate: "https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png",
43 subdomains: ["a", "b", "c"],
44 maxZoom: 17,
45 attribution: "© OpenTopoMap (CC-BY-SA) · © OpenStreetMap contributors",
46 },
47};
48
49export async function run(container: HTMLElement): Promise<RunResultObject> {
50 const view = new MapView({
51 container,
52 renderEngine: new RenderEngine(),
53 mode: "2d",
54 center: [32.866, 39.925],
55 zoom: 6,
56 minZoom: 2,
57 maxZoom: 18,
58 backgroundColor: "transparent",
59 });
60
61
62
63
64
65
66 let active: RasterTileLayer = new RasterTileLayer(PROVIDERS.osm);
67 view.map.add(active);
68
69 await view.when();
70
71 const switchTo = (next: ProviderId) => {
72 const incoming = new RasterTileLayer(PROVIDERS[next]);
73 view.map.remove(active);
74 view.map.add(incoming);
75 active = incoming;
76 };
77
78 return {
79 dispose: () => view.destroy(),
80 controls: [
81 {
82 kind: "segmented",
83 id: "basemap",
84 initial: "osm",
85 options: [
86 { value: "osm", label: "OSM" },
87 { value: "arcgis", label: "Satellite" },
88 { value: "carto-dark", label: "Dark" },
89 { value: "opentopo", label: "Topo" },
90 ],
91 onChange: (value) => switchTo(value as ProviderId),
92 },
93 ],
94 };
95}