rectangle.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var selector;
  2. var firstPointSet = false;
  3. var mouseDown = false;
  4. var camera
  5. export function drawRectangle(Cesium, viewer, screenSpaceEventHandler,cb) {
  6. if (camera) {
  7. return
  8. }
  9. var rectangleSelector = new Cesium.Rectangle();
  10. var screenSpaceEventHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  11. var cartesian = new Cesium.Cartesian3();
  12. var tempCartographic = new Cesium.Cartographic();
  13. var center = new Cesium.Cartographic();
  14. var firstPoint = new Cesium.Cartographic();
  15. camera = viewer.camera;
  16. screenSpaceEventHandler.setInputAction(function drawSelector(movement) {
  17. if (!mouseDown) {
  18. return;
  19. }
  20. cartesian = camera.pickEllipsoid(movement.endPosition, viewer.scene.globe.ellipsoid, cartesian);
  21. if (cartesian) {
  22. //mouse cartographic
  23. tempCartographic = Cesium.Cartographic.fromCartesian(cartesian, Cesium.Ellipsoid.WGS84, tempCartographic);
  24. if (!firstPointSet) {
  25. Cesium.Cartographic.clone(tempCartographic, firstPoint);
  26. firstPointSet = true;
  27. } else {
  28. rectangleSelector.east = Math.max(tempCartographic.longitude, firstPoint.longitude);
  29. rectangleSelector.west = Math.min(tempCartographic.longitude, firstPoint.longitude);
  30. rectangleSelector.north = Math.max(tempCartographic.latitude, firstPoint.latitude);
  31. rectangleSelector.south = Math.min(tempCartographic.latitude, firstPoint.latitude);
  32. selector.show = true;
  33. }
  34. }
  35. }, Cesium.ScreenSpaceEventType.MOUSE_MOVE, Cesium.KeyboardEventModifier.SHIFT);
  36. var getSelectorLocation = new Cesium.CallbackProperty(function getSelectorLocation(time, result) {
  37. return Cesium.Rectangle.clone(rectangleSelector, result);
  38. }, false);
  39. screenSpaceEventHandler.setInputAction(function startClickShift() {
  40. mouseDown = true;
  41. selector.rectangle.coordinates = getSelectorLocation;
  42. }, Cesium.ScreenSpaceEventType.LEFT_DOWN, Cesium.KeyboardEventModifier.SHIFT);
  43. screenSpaceEventHandler.setInputAction(function endClickShift() {
  44. mouseDown = false;
  45. firstPointSet = false;
  46. selector.rectangle.coordinates = rectangleSelector;
  47. cb && cb(rectangleSelector)
  48. }, Cesium.ScreenSpaceEventType.LEFT_UP, Cesium.KeyboardEventModifier.SHIFT);
  49. //Hide the selector by clicking anywhere
  50. screenSpaceEventHandler.setInputAction(function hideSelector() {
  51. selector.show = false;
  52. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  53. selector = viewer.entities.add({
  54. selectable: false,
  55. show: false,
  56. rectangle: {
  57. coordinates: getSelectorLocation,
  58. material: Cesium.Color.RED.withAlpha(0.5)
  59. }
  60. });
  61. }