toolCurrentArea.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. define([
  2. 'html!templates/tools/toolCurrentArea',
  3. 'modDir/service/regionSearch',
  4. 'text!data/address_baike/db.json',
  5. 'css!styles/tools/toolCurrentArea'
  6. ],
  7. function(tplLayout, regionSearchF,addressBaikeDb) {
  8. /**
  9. * 模块状态,用于存储模块的状态 例如:收起,关闭
  10. * @type {Object}
  11. */
  12. var status = {
  13. initialized: false, //是否初始化
  14. modalStatus: false //界面是否弹出
  15. };
  16. /**
  17. * 模块数据 用于数据存储和外部调用
  18. * @type {Object}
  19. * 数据存放
  20. */
  21. var modValue = {
  22. currentAreaDataResult: {},
  23. currentCityInfo: null //当前区域json数据
  24. }
  25. /**
  26. * 初始化并订阅事件
  27. * @return {[type]} [description]
  28. */
  29. function init() {
  30. if (status.initialized === false) {
  31. setLayout();
  32. subscribe();
  33. bindEvent();
  34. getCurrentCity();
  35. hideModal();
  36. status.initialized = true;
  37. addressBaikeDb = JSON.parse(addressBaikeDb);
  38. } else {
  39. if (status.modalStatus) {
  40. hideModal();
  41. } else {
  42. showModal();
  43. }
  44. }
  45. };
  46. function setLayout(){
  47. $("#footer .tools-location").append(tplLayout);
  48. showModal();
  49. };
  50. /**
  51. * 设置地图到指定的区域
  52. * 支持url解析
  53. * @type {Function}
  54. * @param options {Object} {center:中心点,zoom:缩放比例}
  55. */
  56. function mapToPoint(options) {
  57. if (map23DData.display.map2D) {
  58. map2DViewer.setView({
  59. center: options.center,
  60. zoom: options.zoom
  61. });
  62. } else if (map23DData.display.map3D) {
  63. map3DViewer.setView({
  64. center: options.center,
  65. zoom: options.zoom
  66. });
  67. }
  68. setTimeout(function(){
  69. getCurrentCity();
  70. },500);
  71. };
  72. /**
  73. * 显示城市跳转模态窗口
  74. * @type {Function}
  75. */
  76. function showModal() {
  77. $("#footer .Arealinkimg").addClass('downIcon');
  78. // $($("#provinceModal")).css({ left: '10px' });
  79. // $($("#provinceModal")).css({ bottom:'40px' });
  80. $($("#provinceModal")).show();
  81. if(map23DData.display.map2D){
  82. $("#provinceModal .popup_html").removeClass('TD');
  83. }else{
  84. $("#provinceModal .popup_html").addClass('TD');
  85. }
  86. $("#cityPlaceList").mCustomScrollbar({
  87. scrollInertia: 1000
  88. });
  89. $("#cityPlaceList").mCustomScrollbar('update');
  90. status.modalStatus = true;
  91. };
  92. /**
  93. * 隐藏城市跳转模态窗口
  94. * @type {Function}
  95. */
  96. function hideModal() {
  97. $("#footer .Arealinkimg").removeClass('downIcon');
  98. $($("#provinceModal")).hide();
  99. status.modalStatus = false;
  100. };
  101. /**
  102. * 界面布局重置
  103. * @type {Function}
  104. */
  105. function layoutResize() {
  106. $("cityPlaceList").mCustomScrollbar('update');
  107. };
  108. /**
  109. * 获取当前城市信息
  110. * @type {Function}
  111. * @returns {*}
  112. */
  113. function getCurrentCity() {
  114. var center = map23DData.view.center;
  115. if(map23DData.display.map2D){
  116. center = map2DViewer.map.getCenter();
  117. }
  118. var zoom = map23DData.view.zoom;
  119. getPointCity(center, zoom);
  120. };
  121. /**
  122. * 在ONEMAP.M.user中记录当前城市信息
  123. * @type {Function}
  124. * @param data {Object} 城市名称,中心点,bounds范围
  125. * @private
  126. */
  127. function setONEMAPCityInfo(data) {
  128. ONEMAP.D.cityInfo.name = data.name.text;
  129. ONEMAP.D.cityInfo.center = data.center;
  130. ONEMAP.D.cityInfo.pac = data.code;
  131. ONEMAP.D.cityInfo.zoom = data.zoom;
  132. };
  133. /**
  134. * 获取目标城市信息数据
  135. * @type {Function}
  136. * @param center {Object} 中心点
  137. * @param zoom {Number} 缩放比例,不同比例返回的 区域等级不一样
  138. * @returns {*}
  139. * @private
  140. */
  141. function getPointCity(center, zoom) {
  142. var regionSearch = new regionSearchF;
  143. regionSearch.getRegionInfo({ latLng: [center.lat, center.lng], zoom: zoom }, function(data) {
  144. modValue.currentAreaDataResult = data.data;
  145. updateCurrentAreaPanel(data.data);
  146. //updateCurrentAreaWheater(data.data);
  147. });
  148. };
  149. /**
  150. * 更新天气
  151. */
  152. function updateCurrentAreaWheater(){
  153. var cityname = modValue.curAreaName;
  154. // 天气搜索结果
  155. meteo.c.http.httpFunction(meteo.c.http.station, cityname, null, function (json) {
  156. if(json.length>0){
  157. showList(json);
  158. }else{
  159. ONEMAP.C.publisher.publish({ type: 'error', message: '无此区域天气数据' }, 'noteBar::add');
  160. }
  161. })
  162. };
  163. function showList(json){
  164. // console.log(json)
  165. /*meteo.c.popup.showPopup("" + e.currentTarget.id.split("-")[2], json[key].cname, 0,
  166. '2017-12-25 20:00:00', '2017-12-25 08:00:00');*/
  167. }
  168. /**
  169. * 更新 当前城市信息面板 由地图拖动来驱动
  170. * @type {Function}
  171. * @param data {Object}
  172. * @returns {*}
  173. * @private
  174. */
  175. function updateCurrentAreaPanel(data) {
  176. if(!data){
  177. return false;
  178. }
  179. var curAreaName = '';
  180. var curArea = $('#provinceModal .modal-header h3');
  181. curArea.empty();
  182. var nextArea = $('#CurCityInfo');
  183. nextArea.empty();
  184. if (data.hasOwnProperty('level') && data.level.length > 0) {
  185. var i = 0;
  186. var l = data.level.length;
  187. var ABBtn = $('<span class="addressbaike hide"></span>');
  188. for (i; i < l; i++) {
  189. if (i !== (l - 1)) {
  190. var item = $('<a href="javascript:void(0)">' + data.level[i].name.text + '</a> > ');
  191. item.bind("click", { d: data.level[i] }, function(event) {
  192. mapToPoint({
  193. center: [event.data.d.center.lat, event.data.d.center.lon],
  194. zoom: event.data.d.zoom + 3,
  195. name: event.data.d.name.text
  196. });
  197. setONEMAPCityInfo(event.data.d);
  198. });
  199. } else if (i === (l - 1)) {
  200. var item = $('<strong>' + data.level[i].name.text + '</strong>');
  201. modValue.curAreaName = data.level[i].name.text;
  202. curAreaName = data.level[i].name.text;
  203. if(addressBaikeDb[(addPreZero(data.level[i].code))]){
  204. // ABBtn = $('<a class="addressbaike" title="'+curAreaName+'" bid="'+(addPreZero(data.level[i].code))+'">地名百科</a>');
  205. // ABBtn.bind('click',function(){
  206. // var title = $(this).attr("title");
  207. // var bid = $(this).attr("bid");
  208. // require(['modDir/tools/toolAddressBaike'],function(toolAddressBaike){
  209. // toolAddressBaike.init({
  210. // title:title,
  211. // bid:bid
  212. // });
  213. // });
  214. // })
  215. }
  216. //更新ONEMAP.D.cityInfo
  217. setONEMAPCityInfo(data.level[i]);
  218. }
  219. //设置当前 城市信息
  220. if (data.level[i].category === 'city') {
  221. modValue.currentCityInfo = data.level[i];
  222. }
  223. item.appendTo(curArea);
  224. }
  225. $("#curLocationArea").empty().text(curAreaName);
  226. $(".addressbaike").remove();
  227. ABBtn.insertAfter($('.tools-location'));
  228. } else {
  229. curArea.empty().html('<strong>全球</strong>');
  230. }
  231. if (data.hasOwnProperty('next_level') && data.next_level.length > 0) {
  232. var nextLevelAry = sortBy(data.next_level, 'code');
  233. for (var i = 0, l = nextLevelAry.length; i < l; i++) {
  234. var item = $('<a href="javascript:void(0)">' + nextLevelAry[i].name.text + '</a>');
  235. item.bind("click", { d: nextLevelAry[i] }, function(event) {
  236. mapToPoint({
  237. center: [event.data.d.center.lat, event.data.d.center.lon],
  238. zoom: event.data.d.zoom + 3,
  239. name: event.data.d.name.text
  240. });
  241. setONEMAPCityInfo(event.data.d);
  242. });
  243. item.appendTo(nextArea);
  244. }
  245. }
  246. };
  247. function addPreZero(num) {
  248. var t = (num + '').length,
  249. s = '';
  250. for (var i = 0; i < 11 - t; i++) {
  251. s += '0';
  252. }
  253. return num+s;
  254. }
  255. function sortBy(arr, prop, desc) {
  256. var props = [],
  257. ret = [],
  258. i = 0,
  259. len = arr.length;
  260. if (typeof prop == 'string') {
  261. for (; i < len; i++) {
  262. var oI = arr[i];
  263. (props[i] = new String(oI && oI[prop] || ''))._obj = oI;
  264. }
  265. }
  266. props.sort();
  267. for (i = 0; i < len; i++) {
  268. ret[i] = props[i]._obj;
  269. }
  270. if (desc) {
  271. ret.reverse();
  272. }
  273. return ret;
  274. };
  275. function popup_html_css(options){
  276. if(options == '2d'){
  277. $("#provinceModal .popup_html").removeClass('TD');
  278. }else if(options == '3d'){
  279. $("#provinceModal .popup_html").addClass('TD');
  280. }
  281. }
  282. /**
  283. * 注册监听
  284. * @type {Function}
  285. */
  286. function subscribe() {
  287. ONEMAP.C.publisher.subscribe(getCurrentCity, 'mapChange23D');
  288. ONEMAP.C.publisher.subscribe(popup_html_css, 'change23D');
  289. };
  290. /**
  291. * 取消监听
  292. * @type {Function}
  293. */
  294. function unSubscribe() {};
  295. /**
  296. * 监听数据推送
  297. * @type {Function}
  298. */
  299. function publish() {};
  300. /**
  301. * 点击事件绑定
  302. */
  303. function bindEvent() {
  304. $("#provinceModal .modal-header .close").bind("click", function() {
  305. hideModal();
  306. });
  307. $("#footer .location_wheater").bind('click',function(){
  308. updateCurrentAreaWheater();
  309. })
  310. $("#provinceModal .table a").bind("click", function() {
  311. var cityname = $(this).text().replace(':', '');
  312. require(["vendorDir/data/cityData"], function(cityData) {
  313. $.each(cityData, function(index, value) {
  314. if (value.n.indexOf(cityname) >= 0) {
  315. mapToPoint({ center: L.latLng(cityData[index].latLng[0], cityData[index].latLng[1]), zoom: 10 });
  316. return false;
  317. }
  318. });
  319. });
  320. });
  321. $("#provinceModal #CityHotCity a, #provinceModal #globalCity a").bind("click", function() {
  322. var cityname = $(this).text().replace(':', '');
  323. require(["vendorDir/data/cityData"], function(cityData) {
  324. $.each(cityData, function(index, value) {
  325. if (value.n.indexOf(cityname) >= 0) {
  326. mapToPoint({ center: L.latLng(cityData[index].latLng[0], cityData[index].latLng[1]), zoom: 10 });
  327. return false;
  328. }
  329. });
  330. });
  331. });
  332. $("#provinceModal #cityLetterList a").click(function() {
  333. if (L.Browser.ie6) {
  334. return false;
  335. }
  336. $("#provinceModal #cityPlaceList").mCustomScrollbar("scrollTo", $(this).data("role"));
  337. });
  338. //搜索
  339. $('#provinceModalCitySearch .input-small').bind('focus', function(e) {
  340. $(this).val('');
  341. });
  342. $('#provinceModalCitySearch .input-small').bind('keyup', function(e) {
  343. $('#provinceModalCitySearchResult').empty();
  344. var cityname = $(this).val().replace(' ', '');
  345. if (cityname === '') {
  346. $('#provinceModalCitySearchResult').css({ display: 'none' });
  347. return false;
  348. }
  349. require(["vendorDir/data/cityData"], function(cityData) {
  350. var count = 0;
  351. $.each(cityData, function(index, value) {
  352. if (value.n.indexOf(cityname) >= 0) {
  353. var item = $('<li><a href="javascript:void(0)">' + cityData[index].n + '</a></li>').appendTo($('#provinceModalCitySearchResult'));
  354. item.on("click", { data: value }, function(e) {
  355. mapToPoint({ center: L.latLng(e.data.data.latLng[0], e.data.data.latLng[1]), zoom: 10 });
  356. $('#provinceModalCitySearchResult').css({ display: 'none' });
  357. });
  358. count++
  359. if (count > 6) {
  360. return false;
  361. }
  362. }
  363. });
  364. if (count == 0) {
  365. $('<li>请输入正确的城市名</li>').appendTo($('#provinceModalCitySearchResult'))
  366. }
  367. });
  368. $('#provinceModalCitySearchResult').css({ display: 'block' });
  369. });
  370. $('#provinceModalCitySearch .input-small').bind('keydown', function(e) {
  371. if (e.keyCode === 13) {
  372. $('#provinceModalCitySearchResult').empty();
  373. var cityname = $(this).val().replace(' ', '');
  374. if (cityname === '') {
  375. $('#provinceModalCitySearchResult').css({ display: 'none' });
  376. return false;
  377. }
  378. require(["vendorDir/data/cityData"], function(cityData) {
  379. var count = 0;
  380. $.each(cityData, function(index, value) {
  381. if (value.n.indexOf(cityname) >= 0) {
  382. var item = $('<li><a href="javascript:void(0)">' + cityData[index].n + '</a></li>').appendTo($('#provinceModalCitySearchResult'));
  383. item.on("click", { data: value }, function(e) {
  384. mapToPoint({ center: L.latLng(e.data.data.latLng[0], e.data.data.latLng[1]), zoom: 10 });
  385. $('#provinceModalCitySearchResult').css({ display: 'none' });
  386. });
  387. count++
  388. if (count > 6) {
  389. return false;
  390. }
  391. }
  392. });
  393. if (count == 0) {
  394. $('<li>请输入正确的城市名</li>').appendTo($('#provinceModalCitySearchResult'))
  395. }
  396. });
  397. $('#provinceModalCitySearchResult').css({ display: 'block' });
  398. }
  399. });
  400. };
  401. return ONEMAP.M.CurrentArea = {
  402. init:init,
  403. showModal:showModal,
  404. hideModal:hideModal
  405. };
  406. })