cesium点击地图上要素,显示弹窗,一种是共用一个弹窗,一种是多个弹窗,两种方式用于不同需求
更多示例联系QQ:854138497
示例集合1
https://blog.csdn.net/qq_40820382/article/details/132493694
示例集合2
https://blog.csdn.net/qq_40820382/article/details/132594789
一个弹窗示例
代码,直接z在vscode 右键,选择【在浏览器中打开】
<!DOCTYPE html>
<html>
<head>
<title>点击出现弹窗-拖动地图弹窗跟着走</title>
<script src="https://cesium.com/downloads/cesiumjs/releases/1.82/Build/Cesium/Cesium.js"></script>
<link rel="stylesheet" href="https://cesium.com/downloads/cesiumjs/releases/1.82/Build/Cesium/Widgets/widgets.css">
<style>
#cesiumContainer {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
.modal {
display: none;
position: absolute;
z-index: 1000;
background-color: white;
border: 1px solid #888;
padding: 10px;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<div id="cesiumContainer"></div>
<div id="myModal" class="modal">
<span class="close">×</span>
<div id="modal-body"></div>
</div>
<div id="contentToPopUp">
<p>这是要显示在弹窗中的内容。</p>
</div>
<script>
var viewer = new Cesium.Viewer('cesiumContainer', {
imageryProvider : new Cesium.UrlTemplateImageryProvider({
url : 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}'
})
});
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.4074, 39.9042, 10000000),
duration: 0,
});
// 添加一些示例数据作为实体。点加载到中国区域了
var entities = [
viewer.entities.add({
name : 'Example Entity 1',
position : Cesium.Cartesian3.fromDegrees(121.4737, 31.2304), // 上海的经纬度
point : {
pixelSize : 10,
color : Cesium.Color.BLUE
}
}),
// 可以继续添加更多的实体
viewer.entities.add({
name : 'Example Entity 1',
position : Cesium.Cartesian3.fromDegrees(121.4737, 35.2304), // 上海的经纬度
point : {
pixelSize : 10,
color : Cesium.Color.RED
}
}),
];
// 创建一个屏幕空间事件处理器
var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
// 用于存储点击位置
var lastClickPosition = null;
var lon = null;
var lat = null;
handler.setInputAction(function(click) {
const pickedObject = viewer.scene.pick(click.position);
const { ellipsoid } = viewer.scene.globe;
const cartesian = viewer.camera.pickEllipsoid(click.position, ellipsoid);
const cartographic = ellipsoid.cartesianToCartographic(cartesian);
if (Cesium.defined(pickedObject)) {
lon = Cesium.Math.toDegrees(cartographic.longitude);
lat = Cesium.Math.toDegrees(cartographic.latitude);
lastClickPosition = click.position;
// 显示模态框
showPopup(lastClickPosition);
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
var modal = document.getElementById('myModal');
var contentDiv = document.getElementById('contentToPopUp');
function showPopup(position) {
// 清空模态框内容
document.getElementById('modal-body').innerHTML = '';
// 复制内容到模态框
var cloneContent = contentDiv.cloneNode(true);
document.getElementById('modal-body').appendChild(cloneContent);
modal.style.display = "block";
// 设置模态框的位置
updatePopupPosition(position);
// 添加关闭按钮事件监听
document.querySelector('.close').addEventListener('click', function() {
modal.style.display = "none";
});
}
// 更新弹窗位置
function updatePopupPosition(position) {
if (!position || !lastClickPosition) return;
// 获取当前视图的屏幕边界
var clientPosition = viewer.canvas.getBoundingClientRect();
// 计算点击位置相对于浏览器窗口的位置
var x = position.x - clientPosition.left;
var y = position.y - clientPosition.top;
// 设置模态框的位置
modal.style.left = x + 'px';
modal.style.top = y + 'px';
}
// 监听视图变化事件
viewer.scene.postRender.addEventListener(function() {
if (lastClickPosition) {
const Cartesian3Result = Cesium.Cartesian3.fromDegrees(lon, lat);
const Cartesian2Result222 = Cesium.SceneTransforms.wgs84ToWindowCoordinates(viewer.scene,Cartesian3Result,);
updatePopupPosition(Cartesian2Result222);
}
});
// 清理
window.addEventListener('unload', function() {
handler.destroy();
viewer.destroy();
});
</script>
</body>
</html>
多个弹窗示例
<!DOCTYPE html>
<html>
<head>
<title>点击出现弹窗-拖动地图弹窗跟着走-多弹窗</title>
<script src="https://cesium.com/downloads/cesiumjs/releases/1.82/Build/Cesium/Cesium.js"></script>
<link rel="stylesheet" href="https://cesium.com/downloads/cesiumjs/releases/1.82/Build/Cesium/Widgets/widgets.css">
<style>
#cesiumContainer {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
.modal {
display: none;
position: absolute;
z-index: 1000;
background-color: white;
border: 1px solid #888;
padding: 10px;
left: 221px;
font-family: Arial, sans-serif;
}
</style>
</head>
<body id="bodyId">
<div id="cesiumContainer"></div>
<div id="myModal" class="modal">
<span class="close">×</span>
<div id="modal-body"></div>
</div>
<div id="contentToPopUp">
<p>这是要显示在弹窗中的内容。</p>
</div>
<script>
var viewer = new Cesium.Viewer('cesiumContainer', {
imageryProvider : new Cesium.UrlTemplateImageryProvider({
url : 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}'
})
});
Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(80, 26, 150.0, 35.5);
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(116.4074, 39.9042, 10000000),
duration: 0,
});
// 添加一些示例数据作为实体。点加载到中国区域了
var entities = [
viewer.entities.add({
name : 'Example Entity 1',
position : Cesium.Cartesian3.fromDegrees(121.4737, 28.2304), // 上海的经纬度
point : {
pixelSize : 10,
color : Cesium.Color.BLUE
}
}),
// 可以继续添加更多的实体
viewer.entities.add({
name : 'Example Entity 1',
position : Cesium.Cartesian3.fromDegrees(121.4737, 35.2304), // 上海的经纬度
point : {
pixelSize : 10,
color : Cesium.Color.RED
}
}),
];
// 创建一个屏幕空间事件处理器
var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
// 用于存储点击位置
var lastClickPosition = null;
var lon = null;
var lat = null;
const lonlatArr = []
handler.setInputAction(function(click) {
const pickedObject = viewer.scene.pick(click.position);
const { ellipsoid } = viewer.scene.globe;
const cartesian = viewer.camera.pickEllipsoid(click.position, ellipsoid);
const cartographic = ellipsoid.cartesianToCartographic(cartesian);
lon = Cesium.Math.toDegrees(cartographic.longitude);
lat = Cesium.Math.toDegrees(cartographic.latitude);
console.log(111, pickedObject );
if (Cesium.defined(pickedObject)) {
console.log(222, pickedObject );
lonlatArr.push([lon,lat])
lastClickPosition = click.position;
// 显示模态框
showPopup(lastClickPosition);
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
let tempModel = []
let count = 0
var bodyId = document.getElementById('bodyId');
var modal = document.getElementById('myModal');
function showPopup(position) {
count++
var contentDiv = document.getElementById('contentToPopUp');
// 清空模态框内容
document.getElementById('modal-body').innerHTML = '';
// 复制内容到模态框--------这边得新加一个div
var cloneContent = contentDiv.cloneNode(true);
document.getElementById('modal-body').appendChild(cloneContent);
var modalCopy = modal.cloneNode(true);
modalCopy.id = 'myModal' + + Date.now()
bodyId.appendChild(modalCopy)
modalCopy.style.display = "block";
tempModel.push(modalCopy)
// 设置模态框的位置
updatePopupPosition(lonlatArr, count);
// 为每个新创建的弹窗及其关闭按钮绑定特定的事件处理程序。使关闭按钮的事件处理程序只关闭其所属的弹窗
var closeButton = modalCopy.querySelector('.close');
closeButton.addEventListener('click', function() {
modalCopy.style.display = "none";
});
}
// 更新弹窗位置
// function updatePopupPosition(position, count) {
function updatePopupPosition(position, count) {
if (!position || !lastClickPosition) return;
// 获取当前视图的屏幕边界
var clientPosition = viewer.canvas.getBoundingClientRect();
tempModel.forEach((item, index) => {
const Cartesian3Result = Cesium.Cartesian3.fromDegrees(lonlatArr[index][0], lonlatArr[index][1]);
const positionsss = Cesium.SceneTransforms.wgs84ToWindowCoordinates(viewer.scene,Cartesian3Result,);
// 计算点击位置相对于浏览器窗口的位置
var x = positionsss.x - clientPosition.left;
var y = positionsss.y - clientPosition.top;
// 设置模态框的位置
item.style.left = x + 'px';
item.style.top = y + 'px';
})
}
// 监听视图变化事件
viewer.scene.postRender.addEventListener(function() {
if (lastClickPosition) {
// 将经纬度转换为Cartesian3
const Cartesian3Result = Cesium.Cartesian3.fromDegrees(lonlatArr[count-1][0], lonlatArr[count-1][1]);
const Cartesian2Result222 = Cesium.SceneTransforms.wgs84ToWindowCoordinates(viewer.scene,Cartesian3Result,);
updatePopupPosition(lonlatArr, count);
}
});
// 清理
window.addEventListener('unload', function() {
handler.destroy();
viewer.destroy();
});
</script>
</body>
</html>
更多示例联系QQ:854138497
示例集合1
https://blog.csdn.net/qq_40820382/article/details/132493694
示例集合2
https://blog.csdn.net/qq_40820382/article/details/132594789