advancedAddressSearch.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. define(['html!templates/menu/baseCalc/advancedAddressSearch',
  2. 'css!styles/tools/toolPublicPopup',
  3. 'css!styles/menu/baseCalc/advancedAddressSearch',
  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. addressArr: [],
  21. pointStyle: {
  22. color: "#2ecc71",
  23. fontFillColor: "#fff000",
  24. labelOffset: -15,
  25. },
  26. handler: null
  27. };
  28. /**
  29. * 初始化并订阅事件
  30. * @return {[type]} [description]
  31. */
  32. function open(data) {
  33. if (!status.initialized) {
  34. setLayout();
  35. bindEvent();
  36. subscribe();
  37. status.initialized = true;
  38. } else {
  39. // 重置
  40. reset();
  41. }
  42. modValue.data = data;
  43. $("#advancedAddressSearchModal .title").text(data.label);
  44. ONEMAP.C.publisher.publish({
  45. modName: 'advancedAddressSearch'
  46. }, 'baseCalc:active');
  47. };
  48. function setLayout() {
  49. $('body').append(tplLayout);
  50. //拖拽
  51. $("#advancedAddressSearchModal .popup-ct").dragmove($('#advancedAddressSearchModal'));
  52. };
  53. /**
  54. * 事件绑定
  55. */
  56. function bindEvent() {
  57. $("#advancedAddressSearchModal .close").bind('click', function () {
  58. close();
  59. });
  60. $("#advancedAddressSearchModal .btn-default.sure").bind('click', function () {
  61. execute()
  62. });
  63. //回车执行
  64. $('#advancedAddressSearchInput, #jumpToLatInput').bind('keydown', function (e) {
  65. if (e.keyCode === 13) {
  66. execute();
  67. }
  68. });
  69. };
  70. /**
  71. * 注册监听
  72. * @type {Function}
  73. */
  74. function subscribe() {
  75. ONEMAP.C.publisher.subscribe(remove, 'baseCalc:active');
  76. };
  77. /**
  78. * 关闭模块
  79. * @return {[type]} [description]
  80. */
  81. function remove(options) {
  82. if (options.modName != 'advancedAddressSearch') {
  83. close();
  84. } else {
  85. $("#advancedAddressSearchModal").show();
  86. }
  87. }
  88. function reset() {
  89. $("#advancedAddressSearchForm .address").val("");
  90. $("#advancedAddressSearchForm .outputResult table tr").empty("")
  91. clearMarker();
  92. removeMapClickEvent();
  93. }
  94. function close() {
  95. $("#advancedAddressSearchModal").hide();
  96. reset();
  97. }
  98. // 执行
  99. function execute() {
  100. let text = $("#advancedAddressSearchForm .address").val();
  101. getLatLng(text);
  102. }
  103. /**
  104. * 获取并计算经纬度
  105. * @private
  106. */
  107. function getLatLng(text) {
  108. clearMarker();
  109. $.ajax({
  110. type: "get",
  111. dataType: 'json',
  112. url: onemapUrlConfig.PROXY_URL + "/searchByNameV3",
  113. data: {
  114. address: text,
  115. },
  116. success: function (data) {
  117. if (data.code == 200) {
  118. data.content.pois.map(function (item) {
  119. let lon, lat;
  120. item.location.split(",").map(function (num, index) {
  121. if (index == 0) lon = Number(num);
  122. if (index == 1) lat = Number(num);
  123. })
  124. modValue.addressArr.push(addMarker(lon, lat, modValue.pointStyle, item))
  125. })
  126. $("#advancedAddressSearchForm .outputResult table tr").empty()
  127. removeMapClickEvent();
  128. addMapClickEvent();
  129. }
  130. },
  131. error: function (error) {
  132. console.log(error)
  133. }
  134. });
  135. return;
  136. };
  137. function setOutputResult(data) {
  138. $("#advancedAddressSearchForm .outputResult table tr").empty()
  139. let html = "";
  140. html += "<tr><td><span>名称:</span></td><td><span>" + data.name + "</span></td></tr>"
  141. html += "<tr><td><span>地址:</span></td><td><span>" + data.address + "</span></td></tr>"
  142. if (Object.getOwnPropertyNames(data.微网格信息).length >= 3) {
  143. ttttt(data.微网格信息).map(function (item) {
  144. html += item
  145. })
  146. }
  147. if (Object.getOwnPropertyNames(data.村居信息).length >= 3) {
  148. ttttt(data.村居信息).map(function (item) {
  149. html += item
  150. })
  151. }
  152. if (Object.getOwnPropertyNames(data.网格信息).length >= 3) {
  153. ttttt(data.网格信息).map(function (item) {
  154. html += item
  155. })
  156. }
  157. function ttttt(obj) {
  158. let fieldArr = Object.getOwnPropertyNames(obj);
  159. let htmlArr = fieldArr.map(function (field) {
  160. return "<tr><td><span>" + field + ":</span></td><td><span>" + obj[field] + "</span></td></tr>"
  161. })
  162. return htmlArr;
  163. }
  164. $("#advancedAddressSearchForm .outputResult table tr").append(html);
  165. }
  166. function addMarker(lon, lat, style, info) {
  167. let entity = map3DViewer.map.entities.add({
  168. position: Cesium.Cartesian3.fromDegrees(lon, lat, 0),
  169. point: {
  170. color: Cesium.Color.fromCssColorString(style.color), //颜色
  171. pixelSize: 10, //大小
  172. outlineColor: Cesium.Color.YELLOW, //轮廓颜色
  173. outlineWidth: 1
  174. },
  175. type: "advancedAddressSearch",
  176. info: info
  177. // label: {
  178. // pixelOffset: new Cesium.Cartesian2(0, style.labelOffset),
  179. // text: text,
  180. // font: '24px',
  181. // fillColor: Cesium.Color.fromCssColorString(style.color)
  182. // }
  183. })
  184. return entity._id;
  185. }
  186. function addMapClickEvent() {
  187. if (modValue.handler != null) return;
  188. modValue.handler = new Cesium.ScreenSpaceEventHandler(map3DViewer.map.canvas);
  189. // 监听鼠标点击事件
  190. modValue.handler.setInputAction(function (click) {
  191. var pickedObject = map3DViewer.map.scene.pick(click.position);
  192. if (Cesium.defined(pickedObject) && pickedObject.id instanceof Cesium.Entity) {
  193. var entity = pickedObject.id;
  194. if (entity.type == "advancedAddressSearch") {
  195. let info = entity.info;
  196. setOutputResult(info);
  197. }
  198. }
  199. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  200. }
  201. function removeMapClickEvent() {
  202. if (modValue.handler) {
  203. modValue.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
  204. modValue.handler = null;
  205. }
  206. }
  207. /**
  208. * 清除marker
  209. * @return {[type]} [description]
  210. */
  211. function clearMarker() {
  212. if (modValue.addressArr.length > 0) {
  213. modValue.addressArr.map(function (entityId) {
  214. map3DViewer.map.entities.removeById(entityId)
  215. })
  216. modValue.addressArr = []
  217. }
  218. }
  219. return ONEMAP.M.advancedAddressSearchForm = {
  220. open: open,
  221. clearMarker: clearMarker
  222. };
  223. })