三维坐标拾取
鼠标点击场景,可返回三维坐标信息,包括经度纬度和高度,若点击天空,则返回undefined。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>鼠标事件三维坐标拾取</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
#info {
color: #000000;
position: absolute;
padding: 5px;
top: 20px;
left: 20px;
font-size: 22px;
font-weight: bold;
background-color: #cccccc99;
}
</style>
<script src="https://delivery.mapmost.com/cdn/sdk/webgl/v9.2.0/mapmost-webgl-min.js"></script>
</head>
<body>
<div id="map"></div>
<div id="info"></div>
<script>
let map = new mapmost.Map({
container: 'map',
style: "<your style url>",
center: [120.74564892237333, 31.306676689118333],
zoom: 16.5,
pitch: 49.59,
bearing: -82.40,
userId: '***', // 授权码
env3D: {
defaultLight: true,
exposure: 1.0,
envMap: './hdr/env.hdr'
}
});
let modelLayer;
map.on('load', function () {
let models_obj = ["./model/Olympic.glb", "./model/OlympicRoof.glb"].map(item => ({
type: 'glb',
url: item
}));
let options = {
id: 'model_id',
type: 'model',
models: models_obj,
center: [120.74603465203592, 31.30605899929158, 0.0],
callback: function (group, layer) {
modelLayer = layer;
}
};
map.addLayer(options);
map.addSource("building", {
"type": "vector",
"url": "<your data>"
})
map.addLayer({
"id": "buildings-high_",
"type": "fill-extrusion",
"source": "cultural",
"source-layer": "b_buildings",
"minzoom": 0,
"filter": ["all", [">=", "dscs", 5]],
"layout": { "visibility": "visible" },
"paint": {
"fill-extrusion-height": ["get", "jzzgdgd"],
"fill-extrusion-color": {
"stops": [
[
12,
"rgba(252, 254, 255, 1)"
],
[
16,
"rgba(255, 255, 255, 1)"
]
]
},
"fill-extrusion-opacity": 1
}
})
map.on('click', function (e) {
let coordinate = e.coord
modelLayer.addPoints({
type: "cube",
size: 2,
color: 0xff0000,
data: [{
coordinate: coordinate
}]
});
document.getElementById('info').innerText = "三维坐标:" + '[' + coordinate + ']';
})
})
</script>
</body>
</html>