Change map basemap
This example demonstrates how to dynamically change the underlying basemap using the setBasemap() method while ensuring that your splats models remain correctly positioned and visible in the 3D scene.
One of the key strengths of the MapTiler GeoSplats SDK is its ability to handle complex 3D assets, such as splats, regardless of the base map style. Whether you switch from a satellite view to a minimalist base style, your high-fidelity 3D models stay in place, perfectly aligned with the geographic coordinates.
NPM module setup
npm install --save @maptiler/geosplats
import { SplatModel, Map, config } from "@maptiler/geosplats";
import '@maptiler/geosplats/dist/maptiler-geosplats.css';
config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
apiKey: config.apiKey,
container: "map",
center: [-74.42420, 40.77946],
zoom: 17.94,
basemap: "hybrid-v4"
});
map.on("load", () => {
const splatModel = new SplatModel({
model: "YOUR_MAPTILER_SPLAT_MODEL_ID_HERE",
});
map.addSplatModel(splatModel);
const styleSelect = document.getElementById("style-select");
styleSelect.addEventListener("change", (e) => {
const selectedStyle = e.target.value;
map.setBasemap(selectedStyle);
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Map Basemap</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="map"></div>
<div id="ui">
<label for="style-select"><strong>Map Style:</strong></label><br>
<select id="style-select">
<option value="satellite-v4">Satellite</option>
<option value="streets-v4">Streets</option>
<option value="hybrid-v4" selected>Hybrid</option>
<option value="dataviz-v4">Dataviz</option>
<option value="base-v4">Base</option>
</select>
</div>
<script type="module" src="./main.js"></script>
</body>
</html>
body {
font-family: Inter, sans-serif;
margin: 0;
overflow: hidden;
}
#map {
width: 100%;
height: 100vh;
}
#ui {
position: absolute;
top: 10px;
left: 10px;
background: rgba(255, 255, 255, 0.9);
padding: 10px;
border-radius: 4px;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}
select {
padding: 5px;
font-size: 14px;
margin-top: 5px;
}
Learn more
Check out the MapTiler GeoSplats SDK JS reference
Related examples
Add splat model
ExamplesLearn how to load a map using the GeoSplats SDK and add a photorealistic splat model. A splat model offer a modern alternative to 3D meshes, perfect for complex scenes.
Edit model in custom editor
ExamplesBuild a custom 3D editor to adjust the position, rotation, and scale of splat models and save their properties to a database.
Capture events
ExamplesLearn how to capture both global map events and specific splats events like load, update, mouseover, and click.
Opacity and visibility
ExamplesLearn how to dynamically control the transparency and visibility of photorealistic splats objects using the MapTiler GeoSplats SDK.