measureDistance.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. var measureDistance = {
  2. modValue: {
  3. handler: null,
  4. tempPoints: [],
  5. viewer: null,
  6. tempEntities: {},
  7. distanceIndex: 0,
  8. distance: null,
  9. clampToGround: false,
  10. tips: $('<div class="measureDistance_tips map3D">左键添加点,右键结束</div>')
  11. },
  12. init: function (options) {
  13. if (options.action == 'add') {
  14. this.modValue.viewer = options.viewer;
  15. this.modValue.clampToGround = options.clampToGround;
  16. this.addMeasureDistance();
  17. this.modValue.tips.appendTo(this.modValue.viewer._container);
  18. } else if (options.action == 'remove') {
  19. this.removeMeasureDistance();
  20. }
  21. },
  22. addMeasureDistance: function () {
  23. var _this = this;
  24. if (!this.modValue.handler) {
  25. this.modValue.distanceIndex++;
  26. this.modValue.distance = null;
  27. this.modValue.tempEntities[this.modValue.distanceIndex] = [];
  28. this.modValue.handler = new SkyScenery.ScreenSpaceEventHandler(this.modValue.viewer.scene.canvas);
  29. this.modValue.handler.setInputAction(function (event) {
  30. var wp = event.endPosition;
  31. if (!SkyScenery.defined(wp)) {
  32. return;
  33. }
  34. var ray = _this.modValue.viewer.camera.getPickRay(wp);
  35. if (!SkyScenery.defined(ray)) {
  36. return;
  37. }
  38. var cartesian = _this.modValue.viewer.scene.globe.pick(ray, _this.modValue.viewer.scene);
  39. if (!SkyScenery.defined(cartesian)) {
  40. return;
  41. }
  42. $('#' + _this.modValue.viewer._container.id + ' .measureDistance_tips').css({
  43. "left": wp.x + 20,
  44. 'top': wp.y + 10
  45. })
  46. }, SkyScenery.ScreenSpaceEventType.MOUSE_MOVE);
  47. this.modValue.handler.setInputAction(function (click) {
  48. var latlng = _this.screenToLatLng(click.position.x, click.position.y)
  49. if (latlng) {
  50. _this.modValue.tempPoints.push({
  51. lon: latlng.lng,
  52. lat: latlng.lat,
  53. h: latlng.alt,
  54. });
  55. var tempLength = _this.modValue.tempPoints.length;
  56. _this.drawPoint(_this.modValue.tempPoints[_this.modValue.tempPoints.length - 1]);
  57. if (tempLength > 1) {
  58. _this.drawLine(_this.modValue.tempPoints[_this.modValue.tempPoints.length - 2], _this.modValue.tempPoints[_this.modValue.tempPoints.length - 1], true);
  59. }
  60. }
  61. }, SkyScenery.ScreenSpaceEventType.LEFT_CLICK);
  62. this.modValue.handler.setInputAction(function (click) {
  63. _this.removeMeasureDistance('', true);
  64. }, SkyScenery.ScreenSpaceEventType.RIGHT_CLICK);
  65. }
  66. },
  67. removeMeasureDistance: function (msg, callback) {
  68. if (!this.modValue.viewer) {
  69. return;
  70. }
  71. $('#' + this.modValue.viewer._container.id + ' .measureDistance_tips').remove();
  72. if (this.modValue.handler) {
  73. this.modValue.tempPoints = [];
  74. this.modValue.handler.destroy();
  75. this.modValue.handler = null;
  76. }
  77. if (callback) {
  78. this.init('', {
  79. action: 'add',
  80. viewer: this.modValue.viewer
  81. })
  82. }
  83. },
  84. drawLine: function (point1, point2, showDistance) {
  85. var entity =
  86. this.modValue.viewer.entities.add({
  87. polyline: {
  88. clampToGround: true,
  89. positions: [
  90. SkyScenery.Cartesian3.fromDegrees(point1.lon, point1.lat, point1.h),
  91. SkyScenery.Cartesian3.fromDegrees(point2.lon, point2.lat, point2.h)
  92. ],
  93. width: 10.0,
  94. material: new SkyScenery.PolylineGlowMaterialProperty({
  95. color: SkyScenery.Color.CHARTREUSE.withAlpha(.5)
  96. }),
  97. }
  98. });
  99. this.modValue.tempEntities[this.modValue.distanceIndex].push(entity);
  100. if (showDistance) {
  101. var curdistance = this.getFlatternDistance(point1.lat, point1.lon, point2.lat, point2.lon);
  102. if (this.modValue.distance) {
  103. this.modValue.distance = this.modValue.distance + curdistance
  104. } else {
  105. this.modValue.distance = curdistance;
  106. }
  107. if (this.modValue.distance <= 1000) {
  108. var showDistance = this.modValue.distance.toFixed(1) + 'm';
  109. } else {
  110. var showDistance = (this.modValue.distance / 1000).toFixed(3) + 'km';
  111. }
  112. entity =
  113. this.modValue.viewer.entities.add({
  114. position: SkyScenery.Cartesian3.fromDegrees(point2.lon, point2.lat),
  115. label: {
  116. text: showDistance,
  117. heightReference: 2,
  118. font: '22px Helvetica',
  119. fillColor: SkyScenery.Color.WHITE,
  120. pixelOffset: new SkyScenery.Cartesian2(0, -22),
  121. verticalOrigin: SkyScenery.VerticalOrigin.BOTTOM,
  122. }
  123. });
  124. this.modValue.tempEntities[this.modValue.distanceIndex].push(entity);
  125. }
  126. },
  127. drawPoint: function (point) {
  128. var entity =
  129. this.modValue.viewer.entities.add({
  130. position: SkyScenery.Cartesian3.fromDegrees(point.lon, point.lat),
  131. point: {
  132. pixelSize: 10,
  133. heightReference: 2,
  134. color: SkyScenery.Color.RED,
  135. }
  136. });
  137. this.modValue.tempEntities[this.modValue.distanceIndex].push(entity);
  138. },
  139. getFlatternDistance: function (lat1, lng1, lat2, lng2) {
  140. var EARTH_RADIUS = 6378137.0; //单位M
  141. var PI = Math.PI;
  142. function getRad(d) {
  143. return d * PI / 180.0;
  144. }
  145. var f = getRad((lat1 + lat2) / 2);
  146. var g = getRad((lat1 - lat2) / 2);
  147. var l = getRad((lng1 - lng2) / 2);
  148. var sg = Math.sin(g);
  149. var sl = Math.sin(l);
  150. var sf = Math.sin(f);
  151. var s, c, w, r, d, h1, h2;
  152. var a = EARTH_RADIUS;
  153. var fl = 1 / 298.257;
  154. sg = sg * sg;
  155. sl = sl * sl;
  156. sf = sf * sf;
  157. s = sg * (1 - sl) + (1 - sf) * sl;
  158. c = (1 - sg) * (1 - sl) + sf * sl;
  159. w = Math.atan(Math.sqrt(s / c));
  160. r = Math.sqrt(s * c) / w;
  161. d = 2 * w * a;
  162. h1 = (3 * r - 1) / 2 / c;
  163. h2 = (3 * r + 1) / 2 / s;
  164. return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
  165. },
  166. screenToLatLng: function (x, y) {
  167. var pick1 = new SkyScenery.Cartesian2(x, y);
  168. var cartesian = this.modValue.viewer.scene.globe.pick(this.modValue.viewer.camera.getPickRay(pick1), this.modValue.viewer.scene);
  169. if (cartesian) {
  170. var cartographic = this.modValue.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
  171. var lat = SkyScenery.Math.toDegrees(cartographic.latitude);
  172. var lng = SkyScenery.Math.toDegrees(cartographic.longitude);
  173. var alt = cartographic.height;
  174. var position = {
  175. lat: lat,
  176. lng: lng,
  177. alt: alt
  178. }
  179. } else {
  180. var position = false
  181. }
  182. return position;
  183. },
  184. clear: function (msg, options) {
  185. var _this = this
  186. $.each(this.modValue.tempEntities, function (i, t) {
  187. for (var j = 0; j < t.length; j++) {
  188. _this.modValue.viewer.entities.removeById(t[j]._id);
  189. }
  190. })
  191. }
  192. }