-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add altitude support in Marker and Popup (#13335) (h/t @yangtanyu)
- Loading branch information
Showing
8 changed files
with
263 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Mapbox GL JS debug page</title> | ||
<meta charset='utf-8'> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> | ||
<link rel='stylesheet' href='../dist/mapbox-gl.css' /> | ||
<style> | ||
body { margin: 0; padding: 0; } | ||
html, body, #map { height: 100%; } | ||
#controls { position: absolute; top: 0; left: 0; } | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div id='map'></div> | ||
<div id='controls'> | ||
<button id="animate">Animate</button><br /> | ||
<label><input id='terrain-checkbox' type='checkbox'> terrain</label><br /> | ||
<div> | ||
<label>Projection:</label> | ||
<select id="projName"> | ||
<option value="mercator" selected>Mercator</option> | ||
<option value="globe">Globe</option> | ||
<option value="albers">Albers USA</option> | ||
<option value="equalEarth">Equal Earth</option> | ||
<option value="equirectangular">Equirectangular</option> | ||
<option value="lambertConformalConic">Lambert Conformal Conic</option> | ||
<option value="naturalEarth">Natural Earth</option> | ||
<option value="winkelTripel">Winkel Tripel</option> | ||
</select> | ||
</div> | ||
</div> | ||
<script src='../dist/mapbox-gl-dev.js'></script> | ||
<script src='../debug/access_token_generated.js'></script> | ||
<script> | ||
|
||
var map = window.map = new mapboxgl.Map({ | ||
container: 'map', | ||
devtools: true, | ||
zoom: 12.5, | ||
center: [-77.01866, 38.888], | ||
style: 'mapbox://styles/mapbox/streets-v9', | ||
hash: true | ||
}); | ||
|
||
let lngLatArr = []; | ||
const spinningMarkers = []; | ||
['auto', 'map', 'viewport', 'horizon'].forEach((rotationAlignment, i) => { | ||
['auto', 'map', 'viewport'].forEach((pitchAlignment, j) => { | ||
var bounds = map.getBounds(); | ||
var s = bounds.getSouth(); | ||
var n = bounds.getNorth(); | ||
var w = bounds.getWest(); | ||
var e = bounds.getEast(); | ||
|
||
var lng = w + (e - w) * ((i + .5) / 4); | ||
var lat = s + (n - s) * ((j + .5) / 3); | ||
|
||
var height = (i * 2000 + j*2000) * (e - w); | ||
// var height = 0.0; | ||
|
||
|
||
// if(!(i===2 && j===0))return | ||
|
||
const popupHtml = `<div> | ||
Pitch Alignment: ${pitchAlignment}<br> | ||
Rotation Alignment: ${rotationAlignment}<br> | ||
lng:${lng}<br> | ||
lat:${lat}<br> | ||
height:${height}<br> | ||
</div>` | ||
|
||
var popup = new mapboxgl.Popup().setHTML(popupHtml); | ||
|
||
var r = Math.round(Math.random() * 255); | ||
var g = Math.round(Math.random() * 255); | ||
var b = Math.round(Math.random() * 255); | ||
|
||
var sc = Math.random() * 2.5 + 0.5; | ||
|
||
var marker = new mapboxgl.Marker({ | ||
color: `rgb(${r}, ${g}, ${b})`, | ||
scale: sc, | ||
draggable: true, | ||
rotationAlignment, | ||
pitchAlignment, | ||
altitude:height, | ||
}) | ||
.setLngLat([lng, lat]) | ||
.setPopup(popup) | ||
.addTo(map); | ||
|
||
marker.togglePopup(); | ||
|
||
lngLatArr.push({ | ||
lngLat:[lng, lat], | ||
lngOffset:(e - w), | ||
latOffset:(n - s), | ||
height | ||
}) | ||
|
||
spinningMarkers.push(marker); | ||
}); | ||
}); | ||
|
||
|
||
map.on('style.load',()=>{ | ||
addFillExtrusionLayer(lngLatArr) | ||
}) | ||
|
||
|
||
|
||
function addFillExtrusionLayer(data=[]){ | ||
|
||
const dataArr = data.map(a=>{ | ||
let {lngOffset,latOffset} = a; | ||
lngOffset = lngOffset/100; | ||
latOffset = latOffset/100; | ||
const [lng, lat] = a.lngLat; | ||
const aObj = { | ||
...a, | ||
"type": "Feature", | ||
"properties": { | ||
"height": a.height, | ||
"color": `rgba(${a.height%255},${a.height%150},${255 - a.height%255},1)`, | ||
}, | ||
lngLatArr:[ | ||
[lng-lngOffset,lat+latOffset], | ||
[lng+lngOffset,lat+latOffset], | ||
[lng+lngOffset,lat-latOffset], | ||
[lng-lngOffset,lat-latOffset], | ||
] | ||
} | ||
|
||
aObj.geometry = { | ||
"type": "Polygon", | ||
"coordinates": [aObj.lngLatArr] | ||
} | ||
|
||
return aObj | ||
}); | ||
|
||
let layer = { | ||
id:'FillExtrusionLayerId', | ||
'type': 'fill-extrusion', | ||
'source': { | ||
type: 'geojson', | ||
data: { | ||
"type": "FeatureCollection", | ||
"features":dataArr, | ||
} | ||
}, | ||
|
||
'paint': { | ||
'fill-extrusion-opacity':0.6, | ||
// 'fill-extrusion-color': 'rgba(255,10,10,0.8)', | ||
'fill-extrusion-color': ["get", "color"], | ||
'fill-extrusion-height': ["get", "height"], | ||
'fill-extrusion-base': 0, | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
||
map.addLayer(layer); | ||
|
||
} | ||
|
||
let animate = false; | ||
document.getElementById('animate').addEventListener('click', () => { | ||
animate = !animate; | ||
if (animate) { | ||
spinMarkers(); | ||
} | ||
}); | ||
|
||
let rotation = 0; | ||
function spinMarkers() { | ||
rotation++; | ||
spinningMarkers.forEach((marker) => { | ||
marker.setRotation(rotation); | ||
}); | ||
if (animate) { | ||
window.requestAnimationFrame(spinMarkers); | ||
} | ||
} | ||
window.requestAnimationFrame(spinMarkers); | ||
|
||
map.addControl(new mapboxgl.NavigationControl()); | ||
|
||
map.on('load', function() { | ||
map.addSource('mapbox-dem', { | ||
"type": "raster-dem", | ||
"url": "mapbox://mapbox.mapbox-terrain-dem-v1", | ||
"tileSize": 512, | ||
"maxzoom": 14 | ||
}); | ||
|
||
map.setFog({}); | ||
}); | ||
|
||
document.getElementById('terrain-checkbox').onclick = function() { | ||
map.setTerrain(this.checked ? {"source": "mapbox-dem", "exaggeration": 10} : null); | ||
}; | ||
|
||
document.getElementById('projName').addEventListener('change', (e) => { | ||
const el = document.getElementById('projName'); | ||
map.setProjection(el.options[el.selectedIndex].value); | ||
}); | ||
</script> | ||
|
||
</body> | ||
</html> |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.