singleModelBindClick.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ; (function (Cesium) {
  2. checkSkyScenery(Cesium);
  3. function singleModelBindClick(viewer, options) {
  4. var t = {
  5. heading: 0,
  6. pitch: 0,
  7. roll: 0,
  8. info: null,
  9. }
  10. if (options.url != undefined) {
  11. if (typeof options.url != "string") {
  12. throw "url parameter error"
  13. }
  14. } else {
  15. throw "url cannot be empty"
  16. }
  17. if (options.lon != undefined) {
  18. if (typeof options.lon != "number") {
  19. throw "lon parameter error"
  20. }
  21. } else {
  22. throw "lon cannot be empty"
  23. }
  24. if (options.lat != undefined) {
  25. if (typeof options.lat != "number") {
  26. throw "lat parameter error"
  27. }
  28. } else {
  29. throw "lat cannot be empty"
  30. }
  31. if (options.height != undefined) {
  32. if (typeof options.height != "number") {
  33. throw "height parameter error"
  34. }
  35. } else {
  36. throw "height cannot be empty"
  37. }
  38. var option = Object.assign(t, options)
  39. if (viewer.viewerType != "SkyScenery") throw "viewer is error";
  40. this.viewer = viewer;
  41. var position = Cesium.Cartesian3.fromDegrees(option.lon, option.lat, option.height);
  42. var heading = Cesium.Math.toRadians(0);
  43. var pitch = Cesium.Math.toRadians(0);
  44. var roll = Cesium.Math.toRadians(0);
  45. var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
  46. var orientation = Cesium.Transforms.headingPitchRollQuaternion(
  47. position,
  48. hpr
  49. );
  50. var id = guid();
  51. this.id = id
  52. viewer.entities.add({
  53. id: id,
  54. info: option.info,
  55. position: position,
  56. orientation: orientation,
  57. model: {
  58. uri: option.url // "http://122.228.28.40:10092/proxy/sxt.gltf?servertype=CA_ZH_IOT_SXT&token=65463DEE-620A-0ED5-2385-17ECD07CD351",
  59. },
  60. });
  61. }
  62. singleModelBindClick.prototype.openClick = function (callback) {
  63. var that = this;
  64. var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  65. handler.setInputAction(function (click) {
  66. var pick = viewer.scene.pick(click.position);
  67. if (Cesium.defined(pick) && (pick.id.id === that.id)) {
  68. console.log(pick.id.info)
  69. if (callback) callback(pick.id.info);
  70. }
  71. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  72. this.handler = handler;
  73. }
  74. singleModelBindClick.prototype.closeClick = function () {
  75. if (this.handler) {
  76. this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
  77. this.handler = null
  78. delete this.handler
  79. }
  80. }
  81. function guid() {
  82. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  83. var r = Math.random() * 16 | 0,
  84. v = c == 'x' ? r : (r & 0x3 | 0x8);
  85. return v.toString(16);
  86. });
  87. }
  88. Cesium.singleModelBindClick = singleModelBindClick
  89. }(SkyScenery));