targetPoint.js 8.2 KB

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