coorTransform.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. define(['html!templates/menu/baseCalc/coorTransform',
  2. 'css!styles/tools/toolPublicPopup',
  3. 'css!styles/menu/baseCalc/coorTransform',
  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. beforeId: "",
  21. afterId: "",
  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. modValue.data = data;
  39. if (!status.initialized) {
  40. setLayout();
  41. bindEvent();
  42. subscribe();
  43. status.initialized = true;
  44. } else {
  45. // 重置
  46. reset();
  47. }
  48. resetLayout();
  49. $("#coorTransformModal .title").text(data.label);
  50. ONEMAP.C.publisher.publish({
  51. modName: 'coorTransform'
  52. }, 'baseCalc:active');
  53. };
  54. function setLayout() {
  55. $('body').append(tplLayout);
  56. //拖拽
  57. $("#coorTransformModal .popup-ct").dragmove($('#coorTransformModal'));
  58. };
  59. function resetLayout() {
  60. if (modValue.data.coorKey == "sh2000xyToWgs84") {
  61. $("#coorTransformModal .beforeLon").text("X:")
  62. $("#coorTransformModal .beforeLat").text("Y:")
  63. $("#coorTransformModal .afterLonLat").text("经度:")
  64. }
  65. else if (modValue.data.coorKey == "wgs84ToSh2000xy") {
  66. $("#coorTransformModal .beforeLon").text("经度:")
  67. $("#coorTransformModal .beforeLat").text("纬度:")
  68. $("#coorTransformModal .afterLonLat").text("XY:")
  69. } else {
  70. $("#coorTransformModal .beforeLon").text("经度:")
  71. $("#coorTransformModal .beforeLat").text("纬度:")
  72. $("#coorTransformModal .afterLonLat").text("经纬度:")
  73. }
  74. }
  75. /**
  76. * 事件绑定
  77. */
  78. function bindEvent() {
  79. $("#coorTransformModal .close").bind('click', function () {
  80. close();
  81. });
  82. $("#coorTransformModal .btn-default.sure").bind('click', function () {
  83. execute()
  84. });
  85. //回车执行
  86. $('#coorTransformInput, #jumpToLatInput').bind('keydown', function (e) {
  87. if (e.keyCode === 13) {
  88. execute();
  89. }
  90. });
  91. };
  92. /**
  93. * 注册监听
  94. * @type {Function}
  95. */
  96. function subscribe() {
  97. ONEMAP.C.publisher.subscribe(remove, 'baseCalc:active');
  98. };
  99. /**
  100. * 关闭模块
  101. * @return {[type]} [description]
  102. */
  103. function remove(options) {
  104. if (options.modName != 'coorTransform') {
  105. close();
  106. } else {
  107. $("#coorTransformModal").show();
  108. }
  109. }
  110. function reset() {
  111. $("#coorTransformForm .lonCoor").val("");
  112. $("#coorTransformForm .latCoor").val("");
  113. // clearMarker();
  114. $("#coorTransformForm .outputCoor").text("")
  115. }
  116. function close() {
  117. $("#coorTransformModal").hide();
  118. reset();
  119. }
  120. // 执行
  121. function execute() {
  122. // 检测坐标
  123. let lon = $("#coorTransformForm .lonCoor").val()
  124. let lat = $("#coorTransformForm .latCoor").val()
  125. if (lon == "" || lat == "") {
  126. alert("请完善转换参数")
  127. throw "请完善转换参数 "
  128. }
  129. lon = Number(lon)
  130. lat = Number(lat)
  131. if (isNaN(lon) || isNaN(lat)) {
  132. alert("请输入正确的数字格式的坐标")
  133. throw "请输入正确的数字格式的坐标"
  134. }
  135. if (modValue.data.coorKey == "sh2000xyToWgs84") {
  136. getLatLng(lon, lat);
  137. } else {
  138. // 检测坐标范围
  139. if (judge(lon, lat)) {
  140. getLatLng(lon, lat);
  141. }
  142. }
  143. }
  144. /**
  145. * 获取输入信息并跳转到指定的坐标
  146. * @type {Function}
  147. * @private
  148. */
  149. function judge(lng, lat) {
  150. if (!L.Util.verifyLatLng(lat, lng)) {
  151. alert('请正确输入经纬范围(经度0-180、纬度0-90)');
  152. return false;
  153. }
  154. return true;
  155. }
  156. /**
  157. * 获取并计算经纬度
  158. * @private
  159. */
  160. function getLatLng(beforeLon, beforeLat) {
  161. // clearMarker();
  162. let url = ""
  163. // if (modValue.data.coorKey == "wgsThx") url = '/wgs84ToGcj02';
  164. // if (modValue.data.coorKey == "hxTwgs") url = '/gcj02ToWgs84';
  165. // if (modValue.data.coorKey == "wgsTbd") url = '/wgs84ToBD09';
  166. // if (modValue.data.coorKey == "bdTwgs") url = '/bd09ToWgs84';
  167. // if (modValue.data.coorKey == "wgsTsh2000") url = '/wgs84ToSh2000';
  168. // if (modValue.data.coorKey == "sh2000Twgs") url = '/sh2000ToWgs84';
  169. let paramData = {}
  170. if (modValue.data.coorKey == "sh2000xyToWgs84") {
  171. paramData = {
  172. x: beforeLon,
  173. y: beforeLat,
  174. }
  175. } else {
  176. paramData = {
  177. lon: beforeLon,
  178. lat: beforeLat,
  179. }
  180. }
  181. $.ajax({
  182. type: "get",
  183. dataType: 'json',
  184. url: onemapUrlConfig.PROXY_URL + "/" + modValue.data.coorKey, // + '?lon=' + beforeLon + '&lat=' + beforeLat,
  185. data: paramData,
  186. success: function (data) {
  187. let lon, lat;
  188. if (data.x) {
  189. lon = data.x
  190. lat = data.y
  191. } else {
  192. lon = data.lon
  193. lat = data.lat
  194. }
  195. // modValue.beforeId = addMarker(beforeLon, beforeLat, modValue.beforeStyle, modValue.data.beforeLabel)
  196. // modValue.afterId = addMarker(lon, lat, modValue.afterStyle, modValue.data.afterLabel)
  197. $("#coorTransformForm .outputCoor").text(lon + ', ' + lat)
  198. // map3DViewer.setView({
  199. // center: {
  200. // lng: lon,
  201. // lat: lat
  202. // },
  203. // distance: 40,
  204. // heading: 0,//摄像机平面角度 正北为0
  205. // tilt: 0//摄像机倾斜角
  206. // });
  207. },
  208. error: function (error) {
  209. console.log(error)
  210. }
  211. });
  212. return;
  213. };
  214. function addMarker(lon, lat, style, text) {
  215. let entity = map3DViewer.map.entities.add({
  216. name: '添加点',
  217. position: Cesium.Cartesian3.fromDegrees(lon, lat, 0),
  218. point: {
  219. color: Cesium.Color.fromCssColorString(style.color), //颜色
  220. pixelSize: 10, //大小
  221. outlineColor: Cesium.Color.YELLOW, //轮廓颜色
  222. outlineWidth: 1
  223. },
  224. label: {
  225. pixelOffset: new Cesium.Cartesian2(0, style.labelOffset),
  226. text: text,
  227. font: '24px',
  228. fillColor: Cesium.Color.fromCssColorString(style.color)//Cesium.Color.fromCssColorString(style.fontFillColor)
  229. }
  230. })
  231. return entity._id;
  232. }
  233. /**
  234. * 清除marker
  235. * @return {[type]} [description]
  236. */
  237. function clearMarker() {
  238. if (modValue.beforeId) {
  239. map3DViewer.map.entities.removeById(modValue.beforeId)
  240. modValue.beforeId = ""
  241. }
  242. if (modValue.afterId) {
  243. map3DViewer.map.entities.removeById(modValue.afterId)
  244. modValue.afterId = ""
  245. }
  246. }
  247. return ONEMAP.M.coorTransformForm = {
  248. open: open,
  249. clearMarker: clearMarker
  250. };
  251. })