481 lines
18 KiB
HTML
481 lines
18 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<script src="./Build/Cesium/Cesium.js"></script>
|
||
<script src="./js/turf.min.js"></script>
|
||
<link href="./Build/Cesium/Widgets/widgets.css" rel="stylesheet" />
|
||
|
||
<!-- 添加罗盘 -->
|
||
<script>
|
||
var defineProperties = Object.defineProperties;
|
||
</script>
|
||
<script src="./js/nav/CesiumNavigation.umd.js"></script>
|
||
<link href="./js/nav/cesium-navigation.css" rel="stylesheet" />
|
||
|
||
<script src="./custom/tooltip/tooltip.js"></script>
|
||
<link href="./custom/tooltip/tooltip.css" rel="stylesheet" />
|
||
<style>
|
||
@import url(./Build/Cesium/Widgets/widgets.css);
|
||
html,
|
||
body,
|
||
#cesiumContainer {
|
||
width: 100%;
|
||
height: 100%;
|
||
margin: 0;
|
||
padding: 0;
|
||
overflow: hidden;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div id="cesiumContainer"></div>
|
||
<script>
|
||
Cesium.Ion.defaultAccessToken =
|
||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI5ZjRjNTZkNC01NDYxLTRhMjQtOGEwZC1kZjA3YzQ5YTJlZDkiLCJpZCI6MjYwODQsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1ODcxOTMwODN9.prGsSKyAW_9Ow5zHYPhbm3LsQL-ApQw5-5PNJkDaHi8";
|
||
var viewer = new Cesium.Viewer("cesiumContainer", {
|
||
// terrainProvider: Cesium.createWorldTerrain({
|
||
// requestVertexNormal: true, //添加地形光照
|
||
// requestWaterMask: true, //添加水面波浪效果
|
||
// }),
|
||
baseLayerPicker: false, //底图选择器
|
||
mapProjection: new Cesium.WebMercatorProjection(),
|
||
geocoder: false, //位置查找
|
||
homeButton: false, //视图返回初始位置
|
||
sceneModePicker: false, //视角选择器
|
||
navigationHelpButton: false, //导航帮助按钮
|
||
animation: false, //动画控制器
|
||
// // creditContainer:"credit",//版权显示,指定dom对象的id,再通过id样式可以隐藏CESIUM 版本图标
|
||
timeline: false, //时间线
|
||
fullscreenButton: false, //全屏控件
|
||
vrButton: false,
|
||
infoBox: false,
|
||
selectionIndicator: false,
|
||
// shouldAnimate:true,
|
||
scene3DOnly: true, //默认false,若为true,所有几何体实例将仅会在3D模式中渲染(在GPU内存中)
|
||
// terrainProvider: Cesium.createWorldTerrain({
|
||
// requestVertexNormal: true, //添加地形光照
|
||
// requestWaterMask: true, //添加水面波浪效果
|
||
// }),
|
||
});
|
||
viewer.imageryLayers.removeAll();
|
||
// 添加 Mapbox tile provider
|
||
const mapboxAccess =
|
||
"pk.eyJ1IjoicWl1c2hpamllIiwiYSI6ImNsMDNvdDRybDEyc2YzZG9kbWZoY2FuOW0ifQ.4FH-BUupi46Z0zQ-CEm_Ig";
|
||
|
||
// 样式id查看:https://docs.mapbox.com/api/maps/styles/
|
||
// URL格式的 mapbox://styles/:owner/:style, 其中 :owner 是您的 Mapbox 账户名 :style 是样式 ID 。
|
||
// 可选值:streets-v10、outdoors-v10 、light-v9、dark-v9、satellite-v9、satellite-streets-v10
|
||
const mapboxStyleImagery = new Cesium.MapboxStyleImageryProvider({
|
||
styleId: "dark-v9",
|
||
accessToken: mapboxAccess,
|
||
// proxy: new Cesium.DefaultProxy("http://127.0.0.1:8080/proxy/"),
|
||
});
|
||
viewer.imageryLayers.addImageryProvider(mapboxStyleImagery);
|
||
|
||
var options = {};
|
||
// 用于在使用重置导航重置地图视图时设置默认视图控制。接受的值是Cesium.Cartographic 和 Cesium.Rectangle.
|
||
options.defaultResetView = Cesium.Rectangle.fromDegrees(80, 22, 130, 50);
|
||
// 用于启用或禁用罗盘。true是启用罗盘,false是禁用罗盘。默认值为true。如果将选项设置为false,则罗盘将不会添加到地图中。
|
||
options.enableCompass = true;
|
||
// 用于启用或禁用缩放控件。true是启用,false是禁用。默认值为true。如果将选项设置为false,则缩放控件将不会添加到地图中。
|
||
options.enableZoomControls = true;
|
||
// 用于启用或禁用距离图例。true是启用,false是禁用。默认值为true。如果将选项设置为false,距离图例将不会添加到地图中。
|
||
options.enableDistanceLegend = true;
|
||
// 用于启用或禁用指南针外环。true是启用,false是禁用。默认值为true。如果将选项设置为false,则该环将可见但无效。
|
||
options.enableCompassOuterRing = true;
|
||
|
||
CesiumNavigation.umd(viewer, options);
|
||
|
||
var scene = viewer.scene;
|
||
|
||
viewer.camera.flyTo({
|
||
destination: Cesium.Cartesian3.fromDegrees(
|
||
114.89555032,
|
||
32.40536756,
|
||
4000
|
||
),
|
||
orientation: {
|
||
heading: Cesium.Math.toRadians(0),
|
||
pitch: Cesium.Math.toRadians(-90),
|
||
roll: Cesium.Math.toRadians(0),
|
||
},
|
||
});
|
||
|
||
// 绑定事件
|
||
var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
|
||
TooltipDiv.initTool(viewer.cesiumWidget.container);
|
||
// 鼠标移入自定义弹出框
|
||
handler.setInputAction(function (movement) {
|
||
// if (scene.mode !== Cesium.SceneMode.MORPHING) {
|
||
var pickedObject = scene.pick(movement.position);
|
||
console.log(pickedObject, "gggggg");
|
||
if (scene.pickPositionSupported && Cesium.defined(pickedObject)) {
|
||
TooltipDiv.showAt(
|
||
movement.position,
|
||
`<div class="con">名称:${pickedObject.primitive.text}</div>`
|
||
);
|
||
} else {
|
||
TooltipDiv.setVisible(false);
|
||
}
|
||
// }
|
||
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
|
||
|
||
// 加载淮河水系面
|
||
let url =
|
||
"http://localhost:8088/geoserver/allRegion/ows?srsname=EPSG:4326&service=WFS&version=1.0.0&request=GetFeature&typeName=allRegion%3Ajiguan&maxFeatures=50&outputFormat=application%2Fjson";
|
||
fetch(url)
|
||
.then((res) => res.json())
|
||
.then((result) => {
|
||
const primitives = new Cesium.PrimitiveCollection();
|
||
const labelCollection = new Cesium.LabelCollection();
|
||
let colorOption = [0.0, 255, 0.0, 0.8];
|
||
let color = new Cesium.ColorGeometryInstanceAttribute.fromColor(
|
||
Cesium.Color.fromBytes(
|
||
colorOption[0],
|
||
colorOption[1],
|
||
colorOption[2],
|
||
colorOption[3]
|
||
)
|
||
);
|
||
result.features.forEach((feature) => {
|
||
// 添加面要素
|
||
primitives.add(createPrimitiveByFeature1(feature, color));
|
||
// 添加label
|
||
// labelCollection.add(addLabel(feature));
|
||
// addBillboard(feature);
|
||
});
|
||
viewer.scene.primitives.add(primitives);
|
||
viewer.scene.primitives.add(labelCollection);
|
||
});
|
||
|
||
// 定义自定义数据源用于存储poi广告牌
|
||
const dataSource = new Cesium.CustomDataSource("poiIconLabel");
|
||
viewer.dataSources.add(dataSource);
|
||
var poiData = [
|
||
{
|
||
id: 1,
|
||
name: "花果山",
|
||
abstract: "",
|
||
lng: 114.8718,
|
||
lat: 32.39,
|
||
type: "jingdian",
|
||
},
|
||
{
|
||
id: 2,
|
||
name: "天涯海角",
|
||
abstract: "",
|
||
lng: 114.8719,
|
||
lat: 32.3574,
|
||
type: "jingdian",
|
||
},
|
||
{
|
||
id: 3,
|
||
name: "龙岭",
|
||
abstract: "",
|
||
lng: 114.8874,
|
||
lat: 32.3678,
|
||
type: "jingdian",
|
||
},
|
||
|
||
{
|
||
id: 4,
|
||
name: "沙县小吃",
|
||
abstract: "",
|
||
lng: 114.9061,
|
||
lat: 32.3881,
|
||
type: "meishi",
|
||
},
|
||
{
|
||
id: 5,
|
||
name: "兰州拉面",
|
||
abstract: "",
|
||
lng: 114.9037,
|
||
lat: 32.3885,
|
||
type: "meishi",
|
||
},
|
||
{
|
||
id: 6,
|
||
name: "大弗兰",
|
||
abstract: "",
|
||
lng: 114.9034,
|
||
lat: 32.3892,
|
||
type: "meishi",
|
||
},
|
||
{
|
||
id: 7,
|
||
name: "港式烧腊",
|
||
abstract: "",
|
||
lng: 114.9036,
|
||
lat: 32.3869,
|
||
type: "meishi",
|
||
},
|
||
{
|
||
id: 8,
|
||
name: "北京烤鸭",
|
||
abstract: "",
|
||
lng: 114.9031,
|
||
lat: 32.3875,
|
||
type: "meishi",
|
||
},
|
||
{
|
||
id: 9,
|
||
name: "皖厨",
|
||
abstract: "",
|
||
lng: 114.9013,
|
||
lat: 32.3863,
|
||
type: "meishi",
|
||
},
|
||
{
|
||
id: 10,
|
||
name: "老麻雀",
|
||
abstract: "",
|
||
lng: 114.9041,
|
||
lat: 32.3857,
|
||
type: "meishi",
|
||
},
|
||
{
|
||
id: 11,
|
||
name: "吴莊",
|
||
abstract: "",
|
||
lng: 114.9087,
|
||
lat: 32.3889,
|
||
type: "meishi",
|
||
},
|
||
];
|
||
|
||
poiData.forEach((item) => {
|
||
addPoiBillboard(item);
|
||
});
|
||
function addPoiBillboard(data) {
|
||
let imgUrl = "./images/tooltips.png";
|
||
switch (data.type) {
|
||
case "jingdian": {
|
||
imgUrl = "./images/poi/jingdian.png";
|
||
break;
|
||
}
|
||
case "meishi": {
|
||
imgUrl = "./images/poi/meishi.png";
|
||
break;
|
||
}
|
||
}
|
||
dataSource.entities.add({
|
||
position: Cesium.Cartesian3.fromDegrees(data.lng, data.lat),
|
||
billboard: {
|
||
image: imgUrl, // default: undefined
|
||
show: true, // default
|
||
|
||
// eyeOffset: new Cesium.Cartesian3(0.0, 0.0, 0.0), // default
|
||
// horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // default
|
||
// verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // default: CENTER
|
||
// color: Cesium.Color.YELLOW, // default: WHITE
|
||
// rotation: Cesium.Math.PI_OVER_FOUR, // default: 0.0
|
||
// alignedAxis: Cesium.Cartesian3.ZERO, // default
|
||
// heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, //高程引用,设置position与terrain的关系,可以设置是否贴合地形。RELATIVE_TO_GROUND|CLAMP_TO_GROUND|NONE
|
||
|
||
// sizeInMeters: true,
|
||
width: 40, // default: undefined
|
||
height: 42, // default: undefined
|
||
// pixelOffset: new Cesium.Cartesian2(16, -44), // default: (0, 0)
|
||
// pixelOffsetScaleByDistance: new Cesium.NearFarScalar(
|
||
// 10,
|
||
// 0.0,
|
||
// 8.0e6,
|
||
// 1.2
|
||
// ),
|
||
// scale: 1.5,
|
||
// disableDepthTestDistance: 100.0,
|
||
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
|
||
10.0,
|
||
30000.0
|
||
), //根据到相机的距离确定可见性。
|
||
scaleByDistance: new Cesium.NearFarScalar(100, 1.5, 30000, 0.0),
|
||
},
|
||
label: {
|
||
text: data.name,
|
||
font: "24px Helvetica",
|
||
horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // 水平对齐方式
|
||
// verticalOrigin:Cesium.HorizontalOrigin.CENTER,// 垂直对齐方式
|
||
fillColor: Cesium.Color.WHITE,
|
||
outlineColor: Cesium.Color.BLACK,
|
||
outlineWidth: 1,
|
||
showBackground: false,
|
||
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
|
||
scale: 0.8,
|
||
pixelOffset: new Cesium.Cartesian2(0.0, -21.0),
|
||
disableDepthTestDistance: 100.0,
|
||
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
|
||
10.0,
|
||
30000.0
|
||
), //根据到相机的距离确定可见性。
|
||
scaleByDistance: new Cesium.NearFarScalar(100, 1.5, 30000, 0.0),
|
||
},
|
||
});
|
||
}
|
||
function addBillboard(feature) {
|
||
// 计算中心点
|
||
let polygon = feature.geometry.coordinates[0];
|
||
let points = [];
|
||
polygon.forEach((ring) => {
|
||
ring.forEach((coord) => {
|
||
points.push([coord[0], coord[1]]);
|
||
});
|
||
});
|
||
let pointss = turf.points(points);
|
||
let centerResult = turf.center(pointss);
|
||
// console.log(centerResult)
|
||
let center = centerResult.geometry.coordinates;
|
||
|
||
viewer.entities.add({
|
||
position: Cesium.Cartesian3.fromDegrees(center[0], center[1]),
|
||
billboard: {
|
||
image: "./images/tooltips.png", // default: undefined
|
||
show: true, // default
|
||
|
||
// eyeOffset: new Cesium.Cartesian3(0.0, 0.0, 0.0), // default
|
||
// horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // default
|
||
// verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // default: CENTER
|
||
// color: Cesium.Color.YELLOW, // default: WHITE
|
||
// rotation: Cesium.Math.PI_OVER_FOUR, // default: 0.0
|
||
// alignedAxis: Cesium.Cartesian3.ZERO, // default
|
||
// heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, //高程引用,设置position与terrain的关系,可以设置是否贴合地形。RELATIVE_TO_GROUND|CLAMP_TO_GROUND|NONE
|
||
|
||
// sizeInMeters: true,
|
||
width: 150, // default: undefined
|
||
height: 150, // default: undefined
|
||
pixelOffset: new Cesium.Cartesian2(16, -44), // default: (0, 0)
|
||
pixelOffsetScaleByDistance: new Cesium.NearFarScalar(
|
||
1.5e2,
|
||
0.0,
|
||
8.0e6,
|
||
10.0
|
||
),
|
||
// scale: 1.5,
|
||
// disableDepthTestDistance: 100.0,
|
||
// distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
|
||
// 10.0,
|
||
// 30000.0
|
||
// ), //根据到相机的距离确定可见性。
|
||
// scaleByDistance: new Cesium.NearFarScalar(500, 1.5, 30000, 0.0),
|
||
},
|
||
// label: {
|
||
// text: feature.properties["名称"]
|
||
// ? feature.properties["名称"]
|
||
// : "null",
|
||
// font: "24px Helvetica",
|
||
// horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // 水平对齐方式
|
||
// // verticalOrigin:Cesium.HorizontalOrigin.CENTER,// 垂直对齐方式
|
||
// fillColor: Cesium.Color.WHITE,
|
||
// outlineColor: Cesium.Color.BLACK,
|
||
// outlineWidth: 1,
|
||
// showBackground: true,
|
||
// style: Cesium.LabelStyle.FILL_AND_OUTLINE,
|
||
// scale: 0.8,
|
||
// disableDepthTestDistance: 100.0,
|
||
// distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
|
||
// 10.0,
|
||
// 30000.0
|
||
// ), //根据到相机的距离确定可见性。
|
||
// scaleByDistance: new Cesium.NearFarScalar(500, 1, 30000, 0.0),
|
||
// },
|
||
});
|
||
}
|
||
|
||
function addLabel(feature) {
|
||
// 计算中心点
|
||
let polygon = feature.geometry.coordinates[0];
|
||
let points = [];
|
||
polygon.forEach((ring) => {
|
||
ring.forEach((coord) => {
|
||
points.push([coord[0], coord[1]]);
|
||
});
|
||
});
|
||
let pointss = turf.points(points);
|
||
let centerResult = turf.center(pointss);
|
||
// console.log(centerResult)
|
||
let center = centerResult.geometry.coordinates;
|
||
let height = 30;
|
||
// addLine(center,height);
|
||
return {
|
||
position: Cesium.Cartesian3.fromDegrees(center[0], center[1], height),
|
||
text: feature.properties["名称"]
|
||
? feature.properties["名称"]
|
||
: "null",
|
||
font: "24px Helvetica",
|
||
horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // 水平对齐方式
|
||
// verticalOrigin:Cesium.HorizontalOrigin.CENTER,// 垂直对齐方式
|
||
fillColor: Cesium.Color.WHITE,
|
||
outlineColor: Cesium.Color.BLACK,
|
||
outlineWidth: 1,
|
||
showBackground: true,
|
||
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
|
||
scale: 0.8,
|
||
disableDepthTestDistance: 100.0,
|
||
distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
|
||
10.0,
|
||
30000.0
|
||
), //根据到相机的距离确定可见性。
|
||
scaleByDistance: new Cesium.NearFarScalar(500, 1, 30000, 0.0),
|
||
// translucencyByDistance: new Cesium.NearFarScalar(500, 1, 1400, 0.0)
|
||
};
|
||
}
|
||
function addLine(center, height) {
|
||
scene.primitives.add(
|
||
new Cesium.Primitive({
|
||
geometryInstances: new Cesium.GeometryInstance({
|
||
geometry: new Cesium.PolylineGeometry({
|
||
positions: Cesium.Cartesian3.fromDegreesArrayHeights([
|
||
center[0],
|
||
center[1],
|
||
0,
|
||
center[0],
|
||
center[1],
|
||
height,
|
||
]),
|
||
width: 2.0,
|
||
arcType: Cesium.ArcType.RHUMB, //ArcType. GEODESIC, NONE , RHUMB
|
||
// colors:[Cesium.Color.VIOLET,Cesium.Color.WHEAT,Cesium.Color.THISTLE],//指定线段每段的颜色,数量必须与线段数相同
|
||
}),
|
||
attributes: {
|
||
// 通过attributes属性统一指定线段颜色
|
||
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
|
||
new Cesium.Color(44 / 255, 44 / 255, 44 / 255, 0.2)
|
||
),
|
||
},
|
||
}),
|
||
appearance: new Cesium.PolylineColorAppearance({
|
||
translucent: false,
|
||
}),
|
||
})
|
||
);
|
||
}
|
||
|
||
function createPrimitiveByFeature1(feature, color) {
|
||
let coords = feature.geometry.coordinates;
|
||
|
||
let ground = new Cesium.GroundPrimitive({
|
||
geometryInstances: new Cesium.GeometryInstance({
|
||
geometry: new Cesium.PolygonGeometry({
|
||
polygonHierarchy: new Cesium.PolygonHierarchy(
|
||
coords[0][0][0].length == 3
|
||
? Cesium.Cartesian3.fromDegreesArrayHeights(coords.flat(3))
|
||
: Cesium.Cartesian3.fromDegreesArray(coords.flat(3))
|
||
),
|
||
}),
|
||
id: feature.properties,
|
||
attributes: {
|
||
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
|
||
new Cesium.Color(1.0, 0, 1.0, 1.0)
|
||
),
|
||
},
|
||
}),
|
||
appearance: new Cesium.PerInstanceColorAppearance({
|
||
translucent: false,
|
||
}),
|
||
});
|
||
return ground;
|
||
}
|
||
</script>
|
||
</body>
|
||
</html>
|