forked from AJJackGIS/Cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
AJJackGIS
committed
Apr 19, 2019
1 parent
c63aead
commit b63061e
Showing
2 changed files
with
129 additions
and
110 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 |
---|---|---|
@@ -1,123 +1,141 @@ | ||
/** | ||
* 测量线段 | ||
*/ | ||
function measurePolyLine(viewer) { | ||
|
||
var firstPoint = null; // 鼠标单击量测的第一个点 | ||
var firstEntity = null; | ||
var movePoint = null; // 鼠标移动时不断改变的点 | ||
var lineEntity = null; // 鼠标移动时不断改变的线 | ||
var lastPoint = null; // 鼠标单击量测的第二个点 | ||
var lastEntity = null; | ||
var labelEntity = null; // 量测时不断改变的文字信息 | ||
var centerPoint = null; // 量测时不断改变的中心点 | ||
|
||
// 注册鼠标左击事件 | ||
viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) { | ||
var cartesian = viewer.scene.pickPosition(clickEvent.position); // 坐标 | ||
|
||
// 存储第一个点 | ||
if (firstEntity == null) { | ||
firstPoint = cartesian; | ||
firstEntity = viewer.entities.add(new Cesium.Entity({ | ||
position: firstPoint, | ||
point: { | ||
color: Cesium.Color.GREEN, | ||
pixelSize: 10, | ||
scaleByDistance: new Cesium.NearFarScalar(500, 1.0, 2000, 0.0), | ||
translucencyByDistance: new Cesium.NearFarScalar(500, 1.0, 2000, 0.0) | ||
} | ||
})); | ||
function MeasureTools(viewer) { | ||
|
||
// 注册鼠标移动事件 | ||
viewer.screenSpaceEventHandler.setInputAction(function (moveEvent) { | ||
movePoint = viewer.scene.pickPosition(moveEvent.endPosition); // 鼠标移动的点 | ||
var entityCollection = []; | ||
|
||
// 绘制线 | ||
if (lineEntity) { | ||
viewer.entities.remove(lineEntity); | ||
} | ||
lineEntity = viewer.entities.add(new Cesium.Entity({ | ||
polyline: { | ||
positions: [firstPoint, movePoint], | ||
width: 4, | ||
material: Cesium.Color.RED | ||
} | ||
})); | ||
this.getCollection = function () { | ||
return entityCollection; | ||
}; | ||
|
||
// 绘制label | ||
if (labelEntity) { | ||
viewer.entities.remove(labelEntity); | ||
} | ||
// 计算中点 | ||
centerPoint = Cesium.Cartesian3.midpoint(firstPoint, movePoint, new Cesium.Cartesian3()); | ||
// 计算距离 | ||
var length = Cesium.Cartesian3.distance(firstPoint, movePoint); | ||
if (length > 1000) { | ||
length = (length / 1000).toFixed(2) + " km"; | ||
} else { | ||
length = length.toFixed(2) + " m"; | ||
} | ||
labelEntity = viewer.entities.add(new Cesium.Entity({ | ||
position: centerPoint, | ||
label: { | ||
text: "长度:" + length, | ||
showBackground: true, | ||
pixelOffset: new Cesium.Cartesian2(0, -20), | ||
font: '8px sans-serif' | ||
this.destroy = function () { | ||
for (var i = 0; i < entityCollection.length; i++) { | ||
viewer.entities.remove(entityCollection[i]); | ||
} | ||
entityCollection = []; | ||
}; | ||
|
||
this.measurePolyLine = function () { | ||
|
||
var positions = []; | ||
var labelEntity = null; // 标签实体 | ||
|
||
// 注册鼠标左击事件 | ||
viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) { | ||
var cartesian = viewer.scene.pickPosition(clickEvent.position); // 坐标 | ||
|
||
// 存储第一个点 | ||
if (positions.length == 0) { | ||
positions.push(cartesian.clone()); | ||
|
||
addPoint(cartesian); | ||
|
||
// 注册鼠标移动事件 | ||
viewer.screenSpaceEventHandler.setInputAction(function (moveEvent) { | ||
var movePosition = viewer.scene.pickPosition(moveEvent.endPosition); // 鼠标移动的点 | ||
if (positions.length == 2) { | ||
positions.pop(); | ||
positions.push(movePosition); | ||
|
||
// 绘制label | ||
if (labelEntity) { | ||
viewer.entities.remove(labelEntity); | ||
entityCollection.splice(entityCollection.indexOf(labelEntity), 1); | ||
} | ||
|
||
labelEntity = addLabel(positions); | ||
entityCollection.push(labelEntity); | ||
|
||
} else { | ||
positions.push(movePosition); | ||
|
||
// 绘制线 | ||
addLine(positions); | ||
} | ||
})); | ||
|
||
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE); | ||
} else { | ||
// 存储第二个点 | ||
lastPoint = cartesian; | ||
lastEntity = viewer.entities.add(new Cesium.Entity({ | ||
position: lastPoint, | ||
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE); | ||
} else { | ||
// 存储第二个点 | ||
positions.pop(); | ||
positions.push(cartesian); | ||
addPoint(cartesian); | ||
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK); | ||
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE); | ||
} | ||
}, Cesium.ScreenSpaceEventType.LEFT_CLICK); | ||
|
||
|
||
/** | ||
* 添加点 | ||
* @param position | ||
*/ | ||
var addPoint = function (position) { | ||
entityCollection.push(viewer.entities.add(new Cesium.Entity({ | ||
position: position, | ||
point: { | ||
color: Cesium.Color.GREEN, | ||
pixelSize: 10, | ||
scaleByDistance: new Cesium.NearFarScalar(500, 1.0, 2000, 0.0), | ||
translucencyByDistance: new Cesium.NearFarScalar(500, 1.0, 2000, 0.0) | ||
} | ||
})); | ||
}))); | ||
}; | ||
|
||
// 绘制线 | ||
viewer.entities.remove(lineEntity); | ||
lineEntity = viewer.entities.add(new Cesium.Entity({ | ||
/** | ||
* 添加线 | ||
* @param positions | ||
*/ | ||
var addLine = function (positions) { | ||
var dynamicPositions = new Cesium.CallbackProperty(function () { | ||
return positions; | ||
}, false); | ||
entityCollection.push(viewer.entities.add(new Cesium.Entity({ | ||
polyline: { | ||
positions: [firstPoint, lastPoint], | ||
positions: dynamicPositions, | ||
width: 4, | ||
material: Cesium.Color.RED | ||
} | ||
})); | ||
// 绘制label | ||
viewer.entities.remove(labelEntity); | ||
}))); | ||
}; | ||
|
||
/** | ||
* 添加标签 | ||
* @param position | ||
* @param text | ||
*/ | ||
var addLabel = function (positions) { | ||
|
||
// 计算中点 | ||
centerPoint = Cesium.Cartesian3.midpoint(firstPoint, lastPoint, new Cesium.Cartesian3()); | ||
var centerPoint = Cesium.Cartesian3.midpoint(positions[0], positions[1], new Cesium.Cartesian3()); | ||
// 计算距离 | ||
var length = Cesium.Cartesian3.distance(firstPoint, lastPoint); | ||
|
||
if (length > 1000) { | ||
length = (length / 1000).toFixed(2) + " km"; | ||
} else { | ||
length = length.toFixed(2) + " m"; | ||
} | ||
var lengthText = getLengthText(positions[0], positions[1]); | ||
|
||
labelEntity = viewer.entities.add(new Cesium.Entity({ | ||
return viewer.entities.add(new Cesium.Entity({ | ||
position: centerPoint, | ||
label: { | ||
text: "长度:" + length, | ||
text: lengthText, | ||
showBackground: true, | ||
pixelOffset: new Cesium.Cartesian2(0, -20), | ||
font: '8px sans-serif' | ||
} | ||
})); | ||
}; | ||
|
||
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK); | ||
viewer.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE); | ||
} | ||
|
||
/** | ||
* 计算两点距离 | ||
* @param firstPoint | ||
* @param secondPoint | ||
*/ | ||
var getLengthText = function (firstPoint, secondPoint) { | ||
// 计算距离 | ||
var length = Cesium.Cartesian3.distance(firstPoint, secondPoint); | ||
if (length > 1000) { | ||
length = (length / 1000).toFixed(2) + " km"; | ||
} else { | ||
length = length.toFixed(2) + " m"; | ||
} | ||
return length; | ||
}; | ||
} | ||
} | ||
|
||
}, Cesium.ScreenSpaceEventType.LEFT_CLICK); | ||
} |
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