跳到主要内容
版本:9.4.1

通视分析

支持通视分析,可返回障碍点坐标。

show
<!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%;
}

.button.black:hover {
background: -webkit-linear-gradient(top, #818181, #575757);
}

.tool-bar {
position: absolute;
left: 10px;
top: 10px;
z-index: 10000;
}

.param-container {
background-color: rgba(38, 38, 38, 0.75);
padding: 20px 20px 10px 20px;
color: #ffffff;
border: 1px solid #526f82;
}

.button.black {
cursor: pointer;
border: 1px solid #333;
box-shadow: 0 1px 2px #8b8b8b inset, 0 -1px 0 #3d3d3d inset,
0 -2px 3px #8b8b8b inset;
background: -webkit-linear-gradient(top,
rgba(34, 34, 34, 0.49),
rgba(51, 51, 51, 0.55));
}

.button {
line-height: 30px;
text-align: center;
font-weight: bold;
color: #fff;
text-shadow: 1px 1px 1px #333;
border-radius: 3px;
margin: 0 8px 8px 0;
position: relative;
overflow: hidden;
padding: 0px 15px 0px 15px;
}
</style>
<script src="https://delivery.mapmost.com/cdn/sdk/webgl/v9.4.1/mapmost-webgl-min.js"></script>
</head>

<body>
<div id="divButton" class="param-container tool-bar">
<button type="button" id="addViewPoint" class="button black" onclick="isStart=true">
添加观察点
</button>
<button type="button" id="addTargetPoint" class="button black" onclick="addTargetPoint()">
添加目标点
</button>
<button type="button" id="clear" class="button black" onclick="clearAll()">
清除
</button>
</div>
<div id="map"></div>
<script>
let map = new mapmost.Map({
container: 'map',
style: '<your style url>',
bearing: -143.13559342153434,
center: [120.7503930119226, 31.3092452434677],
pitch: 63.28028790989806,
zoom: 17.73148410503527,
userId:'***', // 授权码
});

let modelLayer, sightId;
let startPoint = [];
let endPoint = [];
let pointObject = [];
let isStart = false;
let sightLineAnalysis = new mapmost.SightLineAnalysis(map);

map.on('load', function () {
// 添加模型图层
let options= {
id: 'model_id',
type: 'model',
callback: function (group, layer) {
modelLayer = layer;
},
};
map.addLayer(options);

// 添加倾斜模型
map.addLayer({
type: '3DTiles',
id: 'tile-3d-layer',
data: '<your 3DTiles url>'
});

// 添加圆点
function addPoint(color, point, size) {
let obj = modelLayer.addPoints({
type: 'sphere',
size: size,
color: color,
opacity: 1,
data: [
{
coordinate: point,
},
],
});
pointObject.push(obj);
}

// 添加鼠标点击事件
map.on('click', function (e) {
if (isStart) {
startPoint = map.getPosition(e);
addPoint('#0000ff', startPoint, 2);
} else {
addPoint('#ffb536', endPoint, 2);
addAnalyse();
}
});
});

// 添加通视分析
function addAnalyse() {
let id = `${Math.random() * 10} - ${Math.random() * 5}`;
let result = sightLineAnalysis.analyse({
viewPoint: startPoint,
targetPoint: endPoint,
viewLineColor: '#00ff00',
obstructLineColor: '#ff0000',
drawLine: true,
id,
});
return id;
}

// 根据id清除分析
function clearAnalysis(id) {
sightLineAnalysis.clear(id);
}

// 清除全部分析
function clearAll() {
sightLineAnalysis.clear();
for (let index = 0; index < pointObject.length; index++) {
const element = pointObject[index];
modelLayer.removeModel(element);
}
}

let mouseMoveListener = function (e) {
if (sightId) {
clearAnalysis(sightId);
}
endPoint = map.getPosition(e);
sightId = addAnalyse();
};

// 添加鼠标移动监听事件
function addTargetPoint() {
isStart = false;
map.on('mousemove', mouseMoveListener);
}

// 双击关闭鼠标移动监听事件
map.on('dblclick', function () {
map.off('mousemove', mouseMoveListener);
clearAnalysis(sightId);
});
</script>
</body>
</html>