Last active
March 4, 2022 04:41
-
-
Save malwoodsantoro/b2e1fa9415bdecad346ffe95b5d532fa to your computer and use it in GitHub Desktop.
Add a geocoder to 3D terrain
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> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Add 3D terrain to a map</title> | |
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /> | |
<script src="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.js"></script> | |
<link href="https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css" rel="stylesheet" /> | |
<style> | |
body { | |
margin: 0; | |
padding: 0; | |
} | |
#map { | |
position: absolute; | |
top: 0; | |
bottom: 0; | |
width: 100%; | |
} | |
</style> | |
</head> | |
<body> | |
<script | |
src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.min.js"></script> | |
<link rel="stylesheet" | |
href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.css" | |
type="text/css" /> | |
<div id="map"></div> | |
<script> | |
mapboxgl.accessToken = 'pk.eyJ1IjoibWFsLXdvb2QiLCJhIjoiY2oyZ2t2em50MDAyMzJ3cnltMDFhb2NzdiJ9.X-D4Wvo5E5QxeP7K_I3O8w'; | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
zoom: 13.1, | |
center: [-114.34411, 32.6141], | |
pitch: 85, | |
bearing: 80, | |
style: 'mapbox://styles/mapbox-map-design/ckhqrf2tz0dt119ny6azh975y' | |
}); | |
map.on('load', function () { | |
map.addSource('mapbox-dem', { | |
'type': 'raster-dem', | |
'url': 'mapbox://mapbox.mapbox-terrain-dem-v1', | |
'tileSize': 512, | |
'maxzoom': 14 | |
}); | |
// add the DEM source as a terrain layer with exaggerated height | |
map.setTerrain({ 'source': 'mapbox-dem', 'exaggeration': 1.5 }); | |
// add a sky layer that will show when the map is highly pitched | |
map.addLayer({ | |
'id': 'sky', | |
'type': 'sky', | |
'paint': { | |
'sky-type': 'atmosphere', | |
'sky-atmosphere-sun': [0.0, 0.0], | |
'sky-atmosphere-sun-intensity': 15 | |
} | |
}); | |
map.addControl( | |
new MapboxGeocoder({ | |
accessToken: mapboxgl.accessToken, | |
mapboxgl: mapboxgl, | |
marker: false | |
}) | |
); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment