calcDistance.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. define(['html!templates/menu/baseCalc/calcDistance',
  2. 'css!styles/tools/toolPublicPopup',
  3. 'css!styles/menu/baseCalc/calcDistance',
  4. ],
  5. function (tplLayout) {
  6. /**
  7. * 模块状态,用于存储模块的状态 例如:收起,关闭
  8. * @type {Object}
  9. */
  10. var status = {
  11. initialized: false//是否初始化
  12. };
  13. /**
  14. * 模块数据 用于数据存储和外部调用
  15. * @type {Object}
  16. * 数据存放
  17. */
  18. var modValue = {
  19. data: {},
  20. handler: null,
  21. startPointId: "",
  22. stopPointId: "",
  23. startPointStyle: {
  24. color: "#2ecc71",
  25. fontFillColor: "#fff000",
  26. labelOffset: -15,
  27. },
  28. stopPointStyle: {
  29. color: "#3498db",
  30. fontFillColor: "#fff000",
  31. labelOffset: 15,
  32. }
  33. };
  34. /**
  35. * 初始化并订阅事件
  36. * @return {[type]} [description]
  37. */
  38. function open(data) {
  39. if (!status.initialized) {
  40. setLayout();
  41. bindEvent();
  42. subscribe();
  43. status.initialized = true;
  44. } else {
  45. // 重置
  46. reset();
  47. }
  48. modValue.data = data;
  49. $("#calcDistanceModal .title").text(data.label);
  50. ONEMAP.C.publisher.publish({
  51. modName: 'calcDistance'
  52. }, 'baseCalc:active');
  53. };
  54. function setLayout() {
  55. $('body').append(tplLayout);
  56. //拖拽
  57. $("#calcDistanceModal .popup-ct").dragmove($('#calcDistanceModal'));
  58. };
  59. /**
  60. * 事件绑定
  61. */
  62. function bindEvent() {
  63. $("#calcDistanceModal .close").bind('click', function () {
  64. close();
  65. });
  66. $("#calcDistanceModal .btn-default.sure").bind('click', function () {
  67. execute()
  68. });
  69. $("#calcDistanceModal .btn-default.clickStart").bind('click', function () {
  70. getMapClickEvent(function (lonlat) {
  71. clearSingleMarker(modValue.startPointId, "startPointId")
  72. $("#calcDistanceForm .startPointLon").val(lonlat.lon);
  73. $("#calcDistanceForm .startPointLat").val(lonlat.lat);
  74. modValue.startPointId = addMarker(lonlat.lon, lonlat.lat, modValue.startPointStyle, "起点")
  75. })
  76. });
  77. $("#calcDistanceModal .btn-default.clickStop").bind('click', function () {
  78. getMapClickEvent(function (lonlat) {
  79. clearSingleMarker(modValue.stopPointId, "stopPointId")
  80. $("#calcDistanceForm .stopPointLon").val(lonlat.lon);
  81. $("#calcDistanceForm .stopPointLat").val(lonlat.lat);
  82. modValue.stopPointId = addMarker(lonlat.lon, lonlat.lat, modValue.stopPointStyle, "终点")
  83. })
  84. });
  85. $("#calcDistanceModal .btn-default.cleanStart").bind('click', function () {
  86. $("#calcDistanceForm .startPointLon").val("");
  87. $("#calcDistanceForm .startPointLat").val("");
  88. clearSingleMarker(modValue.startPointId, "startPointId")
  89. });
  90. $("#calcDistanceModal .btn-default.clearStop").bind('click', function () {
  91. $("#calcDistanceForm .stopPointLon").val("");
  92. $("#calcDistanceForm .stopPointLat").val("");
  93. clearSingleMarker(modValue.stopPointId, "stopPointId")
  94. });
  95. //回车执行
  96. $('#calcDistanceInput, #jumpToLatInput').bind('keydown', function (e) {
  97. if (e.keyCode === 13) {
  98. execute();
  99. }
  100. });
  101. };
  102. function getMapClickEvent(callback) {
  103. if (modValue.handler != null) return;
  104. modValue.handler = new Cesium.ScreenSpaceEventHandler(map3DViewer.map.canvas);
  105. // 监听鼠标点击事件
  106. modValue.handler.setInputAction(function (click) {
  107. // 使用pick函数获取点击位置的实际位置
  108. // var cartesian = map3DViewer.map.scene.pickPosition(click.position);
  109. // var cartesian = map3DViewer.map.camera.pickEllipsoid(click.position);
  110. let cartesian = map3DViewer.map.scene.globe.pick(map3DViewer.map.camera.getPickRay(click.position), map3DViewer.map.scene);
  111. if (Cesium.defined(cartesian)) {
  112. // 将笛卡尔坐标转换为经纬度坐标
  113. var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
  114. var longitudeString = Cesium.Math.toDegrees(cartographic.longitude).toFixed(6);
  115. var latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(6);
  116. // var heightString = cartographic.height.toFixed(2);
  117. // console.log('经度:' + longitudeString + ',纬度:' + latitudeString + ',高度:' + heightString)
  118. }
  119. if (isNaN(longitudeString) || isNaN(latitudeString)) {
  120. alert("当前获取坐标有误,请重新获取")
  121. removeMapClickEvent();
  122. throw "当前获取坐标有误,请重新获取";
  123. }
  124. callback({
  125. lon: Number(longitudeString),
  126. lat: Number(latitudeString)
  127. })
  128. removeMapClickEvent();
  129. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  130. modValue.handler.setInputAction(function (click) {
  131. removeMapClickEvent();
  132. }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  133. }
  134. function removeMapClickEvent() {
  135. if (modValue.handler) {
  136. modValue.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
  137. modValue.handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  138. modValue.handler = null;
  139. }
  140. }
  141. /**
  142. * 注册监听
  143. * @type {Function}
  144. */
  145. function subscribe() {
  146. ONEMAP.C.publisher.subscribe(remove, 'baseCalc:active');
  147. };
  148. /**
  149. * 关闭模块
  150. * @return {[type]} [description]
  151. */
  152. function remove(options) {
  153. if (options.modName != 'calcDistance') {
  154. close();
  155. } else {
  156. $("#calcDistanceModal").show();
  157. }
  158. }
  159. function reset() {
  160. $("#calcDistanceForm .startPointLon").val("");
  161. $("#calcDistanceForm .startPointLat").val("");
  162. $("#calcDistanceForm .stopPointLon").val("");
  163. $("#calcDistanceForm .stopPointLat").val("");
  164. $("#calcDistanceForm .outputResult").text("")
  165. clearMarker();
  166. removeMapClickEvent();
  167. }
  168. function close() {
  169. $("#calcDistanceModal").hide();
  170. reset();
  171. }
  172. // 执行
  173. function execute() {
  174. // 检测坐标
  175. let startPointLon = $("#calcDistanceForm .startPointLon").val()
  176. let startPointLat = $("#calcDistanceForm .startPointLat").val()
  177. let stopPointLon = $("#calcDistanceForm .stopPointLon").val()
  178. let stopPointLat = $("#calcDistanceForm .stopPointLat").val()
  179. if (startPointLon == "" || startPointLat == "" || stopPointLon == "" || stopPointLat == "") {
  180. alert("请完善转换参数")
  181. throw "请完善转换参数 "
  182. }
  183. startPointLon = Number(startPointLon)
  184. startPointLat = Number(startPointLat)
  185. stopPointLon = Number(stopPointLon)
  186. stopPointLat = Number(stopPointLat)
  187. if (isNaN(startPointLon) || isNaN(startPointLat) || isNaN(stopPointLon) || isNaN(stopPointLat)) {
  188. alert("请输入正确的数字格式")
  189. throw "请输入正确的数字格式"
  190. }
  191. // 检测坐标范围
  192. if (judge(startPointLon, startPointLat) && judge(stopPointLon, stopPointLat)) {
  193. // 根据转换类型,访问接口获取转换后的数据
  194. // 添加 转前点,转后点
  195. getResult(startPointLon, startPointLat, stopPointLon, stopPointLat);
  196. }
  197. }
  198. /**
  199. * 获取输入信息并跳转到指定的坐标
  200. * @type {Function}
  201. * @private
  202. */
  203. function judge(lng, lat) {
  204. if (!L.Util.verifyLatLng(lat, lng)) {
  205. alert('请正确输入经纬范围(经度0-180、纬度0-90)');
  206. return false;
  207. }
  208. return true;
  209. }
  210. /**
  211. * 获取并计算经纬度
  212. * @private
  213. */
  214. function getResult(startPointLon, startPointLat, stopPointLon, stopPointLat) {
  215. $.ajax({
  216. type: "get",
  217. dataType: 'json',
  218. url: onemapUrlConfig.PROXY_URL + "/calculateDistance",
  219. data: {
  220. lon1: startPointLon,
  221. lat1: startPointLat,
  222. lon2: stopPointLon,
  223. lat2: stopPointLat,
  224. type: 0
  225. },
  226. success: function (data) {
  227. if (data.code == 200) {
  228. $("#calcDistanceForm .outputResult").text(data.content.toFixed(6) + "米")
  229. }
  230. },
  231. error: function (error) {
  232. console.log(error)
  233. }
  234. });
  235. return;
  236. };
  237. function addMarker(lon, lat, style, text) {
  238. let entity = map3DViewer.map.entities.add({
  239. name: '添加点',
  240. position: Cesium.Cartesian3.fromDegrees(lon, lat, 0),
  241. point: {
  242. color: Cesium.Color.fromCssColorString(style.color), //颜色
  243. pixelSize: 10, //大小
  244. outlineColor: Cesium.Color.YELLOW, //轮廓颜色
  245. outlineWidth: 1
  246. },
  247. label: {
  248. pixelOffset: new Cesium.Cartesian2(0, style.labelOffset),
  249. text: text,
  250. font: '24px',
  251. fillColor: Cesium.Color.fromCssColorString(style.color)
  252. }
  253. })
  254. return entity._id;
  255. }
  256. /**
  257. * 清除marker
  258. * @return {[type]} [description]
  259. */
  260. function clearMarker() {
  261. if (modValue.startPointId) {
  262. map3DViewer.map.entities.removeById(modValue.startPointId)
  263. modValue.startPointId = ""
  264. }
  265. if (modValue.stopPointId) {
  266. map3DViewer.map.entities.removeById(modValue.stopPointId)
  267. modValue.stopPointId = ""
  268. }
  269. }
  270. function clearSingleMarker(id, type) {
  271. if (id) {
  272. map3DViewer.map.entities.removeById(id)
  273. modValue[type] = ""
  274. return;
  275. }
  276. }
  277. return ONEMAP.M.calcDistanceForm = {
  278. open: open,
  279. clearMarker: clearMarker
  280. };
  281. })