Created
May 8, 2018 12:20
-
-
Save AlexZeller/4e4f54fab055f76944e348f757987094 to your computer and use it in GitHub Desktop.
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
var viewer = new Cesium.Viewer('cesiumContainer'); | |
var screenSpaceEventHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); | |
var startPoint = new Cesium.Cartographic(); | |
var endPoint = new Cesium.Cartographic(); | |
// Event handler for left click | |
screenSpaceEventHandler.setInputAction(function(click) { | |
// get position of click | |
var clickPosition = viewer.camera.pickEllipsoid(click.position); | |
// Event handler for mouse movement | |
screenSpaceEventHandler.setInputAction(function(movement) { | |
// get position of mouse | |
var mousePosition = viewer.camera.pickEllipsoid(movement.endPosition, viewer.scene.globe.ellipsoid, mousePosition); | |
// create an empty array where the coordinates of the line will be stored | |
var lineArray = []; | |
// convert mouse and click position to lon/lat coordinates and add them to the array | |
var cartographicMouse = Cesium.Ellipsoid.WGS84.cartesianToCartographic(mousePosition); | |
var cartographicClick = Cesium.Ellipsoid.WGS84.cartesianToCartographic(clickPosition); | |
startPoint.longitude = cartographicClick.longitude; | |
startPoint.latitude = cartographicClick.latitude; | |
endPoint.longitude = cartographicMouse.longitude; | |
endPoint.latitude = cartographicMouse.latitude; | |
var x = Cesium.Math.toDegrees(cartographicMouse.longitude); | |
var y = Cesium.Math.toDegrees(cartographicMouse.latitude); | |
lineArray.push(x); | |
lineArray.push(y); | |
x = Cesium.Math.toDegrees(cartographicClick.longitude); | |
y = Cesium.Math.toDegrees(cartographicClick.latitude); | |
lineArray.push(x); | |
lineArray.push(y); | |
if (lineArray.length > 4) { | |
lineArray.shift(); | |
lineArray.shift(); | |
} | |
var linePosition = new Cesium.Cartesian3.fromDegreesArray(lineArray); | |
// draw tempLine | |
var tempLine = viewer.entities.add({ | |
polyline: { | |
positions: linePosition, | |
width: 5, | |
material: Cesium.Color.RED | |
} | |
}); | |
// Event handler for second click; draw line with position from variable linePosition | |
screenSpaceEventHandler.setInputAction(function(click) { | |
// get position of click | |
var clickPosition = viewer.camera.pickEllipsoid(click.position); | |
var Line = viewer.entities.add({ | |
polyline: { | |
positions: linePosition, | |
width: 5, | |
material: Cesium.Color.RED | |
} | |
}); | |
screenSpaceEventHandler = screenSpaceEventHandler && screenSpaceEventHandler.destroy(); | |
}, Cesium.ScreenSpaceEventType.LEFT_CLICK); | |
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE); | |
}, Cesium.ScreenSpaceEventType.LEFT_CLICK); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment