Permalink
Browse files

Add a basic handler for Cesium mouse events

  • Loading branch information...
markerikson committed Mar 2, 2017
1 parent 3fbd2b5 commit ab9b68ba198e0a207711525b90d1cc3ae4ce3245
Showing with 70 additions and 1 deletion.
  1. +67 −0 src/cesium/CesiumClickHandler.jsx
  2. +3 −1 src/utils/utils.js
@@ -0,0 +1,67 @@
+import {Component} from "react";
+
+import ScreenSpaceEventHandler from "cesium/Source/Core/ScreenSpaceEventHandler";
+import SSET from "cesium/Source/Core/ScreenSpaceEventType";
+import CesiumMath from "cesium/Source/Core/Math";
+
+import {noop} from "utils/utils";
+
+export default class CesiumClickHandler extends Component {
+ static defaultProps = {
+ onLeftClick : noop
+ }
+
+ componentDidMount() {
+ const {scene} = this.props;
+
+ if(scene && scene.canvas) {
+ this.screenEvents = new ScreenSpaceEventHandler(scene.canvas);
+ this.createInputHandlers();
+ }
+ }
+
+ componentWillUnmount() {
+ if(this.screenEvents && !this.screenEvents.isDestroyed()) {
+ this.screenEvents.destroy();
+ }
+ }
+
+ createInputHandlers() {
+ this.screenEvents.setInputAction(this.onMouseLeftClick, SSET.LEFT_CLICK);
+ }
+
+ onMouseLeftClick = (e) => {
+ const {position : clientPos} = e;
+ const mapCoordsRadians = this.pickMapCoordinates(clientPos);
+
+ if(mapCoordsRadians) {
+ const mapCoordsDegrees = {
+ lat : CesiumMath.toDegrees(mapCoordsRadians.latitude),
+ lon : CesiumMath.toDegrees(mapCoordsRadians.longitude),
+ };
+
+ this.props.onLeftClick(mapCoordsDegrees);
+ }
+ }
+
+
+ pickMapCoordinates(screenPos) {
+ const {scene} = this.props;
+ let mapCoords;
+
+ if(scene) {
+ const cartesianPos = scene.camera.pickEllipsoid(screenPos);
+
+ if(cartesianPos) {
+ mapCoords = scene.globe.ellipsoid.cartesianToCartographic(cartesianPos);
+ }
+ }
+
+ return mapCoords;
+ }
+
+
+ render() {
+ return null;
+ }
+}
View
@@ -20,4 +20,6 @@ export function shallowEqual(objA, objB) {
}
return true;
-};
+};
+
+export const noop = () => {};

0 comments on commit ab9b68b

Please sign in to comment.