mapHolder.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**
  2. * [ONEMAP.M.mapHolder]
  3. * @return {[object]}
  4. * 地图框架引入
  5. * 地图操作
  6. */
  7. define([
  8. 'modDir/service/addressSearch',
  9. 'modDir/service/poiSearch',
  10. 'modDir/service/regionSearch',
  11. 'modDir/service/routeSearch',
  12. 'css!scripts/vendor/map23dlib/map23DView',
  13. 'css!styles/mapHolder'
  14. ], function (addressSearchF, poiSearchF, regionSearchF, routeSearchF) {
  15. /**
  16. * 地图初始化 根据客户端的类型 初始pc端和移动端
  17. * @exports ONEMAP.M.mapHolder
  18. * @type {Object}
  19. */
  20. var modValue = {
  21. mainLayers: null,
  22. leadorStreetViewControl: null,
  23. vact: 'add',
  24. beifen: {}
  25. }
  26. /**
  27. * 地图初始化参数
  28. */
  29. var options = {
  30. center: [39.90743, 116.39134],
  31. zoom: 6
  32. };
  33. //模块状态
  34. var status = {}
  35. /**
  36. * 初始化
  37. * @type {Function}
  38. * @returns {*}
  39. */
  40. function init() {
  41. if (ONEMAP.D.globalSettingData) {
  42. options.center = [
  43. ONEMAP.D.globalSettingData.baseMap2D.center.lat,
  44. ONEMAP.D.globalSettingData.baseMap2D.center.lng
  45. ];
  46. options.zoom = ONEMAP.D.globalSettingData.baseMap2D.zoom;
  47. }
  48. if (Detector.webgl) {
  49. modValue.webgl = true;
  50. } else {
  51. modValue.webgl = false;
  52. }
  53. mapInit();
  54. subscribe();
  55. ONEMAP.C.publisher.publish('mapDragend', 'mapChange');
  56. //移除加载动画
  57. if (navigator.appName == "Microsoft Internet Explorer") {
  58. $('#loadingGif').fadeOut(200, function () {
  59. $('#loadingTop').animate({
  60. top: '0px'
  61. }, 600);
  62. $('#loadingBottom').animate({
  63. top: '100%'
  64. }, 600);
  65. $('#loadingPanel').hide(400);
  66. setTimeout(function () {
  67. }, 600)
  68. });
  69. } else {
  70. setTimeout(function () {
  71. $('#loadingPanel').addClass('open');
  72. setTimeout(function () {
  73. $('#loadingPanel').fadeOut(400);
  74. }, 1000)
  75. }, 200)
  76. }
  77. };
  78. function mapInit() {
  79. if (modValue.webgl) {
  80. //地图加载
  81. map23DControl.init({
  82. mapWrapId: 'mapHolder',
  83. view: {
  84. center: {
  85. lat: options.center[0],
  86. lng: options.center[1]
  87. },
  88. zoom: options.zoom
  89. },
  90. synch: true,
  91. display: {
  92. map2D: false,
  93. map3D: true
  94. }
  95. });
  96. require(['modDir/3dEarth'], function (tdEarth) {
  97. tdEarth.init();
  98. });
  99. } else {
  100. //地图加载
  101. map23DControl.init({
  102. mapWrapId: 'mapHolder',
  103. view: {
  104. center: {
  105. lat: options.center[0],
  106. lng: options.center[1]
  107. },
  108. zoom: options.zoom
  109. },
  110. synch: false,
  111. display: {
  112. map2D: false,
  113. map3D: true
  114. }
  115. });
  116. }
  117. map23DControl.show3D();
  118. // $('#map3DWrap').css({
  119. // height: $(window).height()
  120. // });
  121. //设置地图边界
  122. map2DViewer.map.setMaxBounds([
  123. [84, 360],
  124. [-84, -360]
  125. ]);
  126. map3DViewer.map.scene.imageryLayers.addImageryProvider(
  127. new Cesium.SingleTileImageryProvider({
  128. url: "./images/world5km.webp",
  129. rectangle: Cesium.Rectangle.fromDegrees(-180.0, -90.0, 180.0, 90.0)
  130. })
  131. )
  132. if (ONEMAP.D.globalSettingData) {
  133. switch (ONEMAP.D.globalSettingData.baseMap2D.type) {
  134. case 1:
  135. if (Detector.webgl) {
  136. map23DControl.setDefaultTileLayer("gm");
  137. } else {
  138. map2DViewer.setDefaultTileLayer("gm");
  139. }
  140. modValue.mainLayers = "gm";
  141. $("#mapBaseLayerControl .choosemap .name-dem-control .name-control").hide();
  142. $("#mapBaseLayerControl .choosemap .name-dem-control .dem-control").hide();
  143. break;
  144. case 2:
  145. if (Detector.webgl) {
  146. map23DControl.setDefaultTileLayer("gr");
  147. } else {
  148. map2DViewer.setDefaultTileLayer("gr");
  149. }
  150. modValue.mainLayers = "gr";
  151. setTimeout(function () {
  152. $("#mapBaseLayerControl .choosemap .name-dem-control .name-control").show();
  153. $("#mapBaseLayerControl .choosemap .name-dem-control .dem-control").show();
  154. }, 500)
  155. break;
  156. case 3:
  157. if (Detector.webgl) {
  158. map23DControl.setDefaultTileLayer("gt");
  159. } else {
  160. map2DViewer.setDefaultTileLayer("gt");
  161. }
  162. modValue.mainLayers = "gt";
  163. $("#mapBaseLayerControl .choosemap .name-dem-control .name-control").hide();
  164. $("#mapBaseLayerControl .choosemap .name-dem-control .dem-control").hide();
  165. break;
  166. }
  167. }
  168. map2DViewer.map.on('dragend', function () {
  169. var zoom = map2DViewer.map.getZoom();
  170. ONEMAP.C.publisher.publish('mapDragend', 'mapChange');
  171. ONEMAP.C.publisher.publish(map2DViewer.map.getZoom(), 'mapChange23D');
  172. })
  173. map2DViewer.map.on('moveend', function (e) {
  174. ONEMAP.C.publisher.publish('mapMove', 'map2DMoveEnd');
  175. })
  176. map2DViewer.map.on('move', function (e) {
  177. ONEMAP.C.publisher.publish('mapMoveIng', 'map2DMoveIng');
  178. })
  179. //操作地图关闭侧栏
  180. map2DViewer.map.on('click dragend', function () {
  181. ONEMAP.C.publisher.publish('mapDragend', 'layout::sideBar');
  182. $("#openTdSideContent").show();
  183. $("#mapHolder").unbind('mouseover');
  184. })
  185. map2DViewer.map.on('mousemove', function (e) {
  186. var lat = e.latlng.lat;
  187. var lng = e.latlng.lng;
  188. ONEMAP.C.publisher.publish({
  189. lat: lat,
  190. lng: lng,
  191. e: e
  192. }, 'mousemove23D');
  193. })
  194. map2DViewer.map.on('zoomend', function () {
  195. if (map2DViewer.map.getZoom() == map2DViewer.map.options.maxZoom) {
  196. ONEMAP.C.publisher.publish({
  197. type: 'warning',
  198. message: '已达地图最大显示级别'
  199. }, 'noteBar::add');
  200. }
  201. if (map2DViewer.map.getZoom() == map2DViewer.map.options.minZoom) {
  202. ONEMAP.C.publisher.publish({
  203. type: 'warning',
  204. message: '已达地图最小显示级别'
  205. }, 'noteBar::add');
  206. }
  207. ONEMAP.C.publisher.publish(map2DViewer.map.getZoom(), 'userHeatlayerMarker');
  208. ONEMAP.C.publisher.publish(map2DViewer.map.getZoom(), 'mapChange23D');
  209. ONEMAP.C.publisher.publish('zoomend', 'mapChange');
  210. })
  211. //地图点击事件
  212. map2DViewer.map.on('click', function (e) {
  213. var lat = e.latlng.lat,
  214. lng = e.latlng.lng;
  215. var options = {
  216. lng: lng,
  217. lat: lat
  218. }
  219. ONEMAP.C.publisher.publish(options, 'mapClick2D');
  220. });
  221. };
  222. function delMarker(ajaxData) {
  223. $.ajax({
  224. type: "get",
  225. dataType: 'json',
  226. url: onemapUrlConfig.userCenterUrl + '/bookmarker/del',
  227. data: ajaxData,
  228. success: function (data) {
  229. //console.log(data)
  230. }
  231. })
  232. }
  233. /**
  234. * 设置经纬网
  235. */
  236. function setSTLatLngLayer(options) {
  237. var chrome = navigator.userAgent.toLowerCase().match(/chrome/) != null || navigator.userAgent.toLowerCase().match(/firefox/) != null;
  238. if (options == 'show') {
  239. STLatLngLayerControl.changStatus(true);
  240. if (!chrome) {
  241. // locaSpace.SetLatLonGridVisible(true);
  242. // locaSpaceMap.Refresh()
  243. }
  244. } else {
  245. STLatLngLayerControl.changStatus(false);
  246. if (!chrome) {
  247. //locaSpace.SetLatLonGridVisible(false);
  248. //locaSpaceMap.Refresh()
  249. }
  250. }
  251. };
  252. /**
  253. * 右键菜单函数
  254. */
  255. //
  256. function disable() {
  257. return false
  258. };
  259. //从这里出发
  260. function pointStartli(e) {
  261. require(['modDir/tools/toolRouteSearch'], function (toolRouteSearch) {
  262. toolRouteSearch.setStartPoint(e.latlng);
  263. });
  264. };
  265. //添加途经点
  266. function pointAcrossli(e) {
  267. require(['modDir/tools/toolRouteSearch'], function (toolRouteSearch) {
  268. toolRouteSearch.setAcrossPoint(e.latlng);
  269. });
  270. };
  271. //添加规避点
  272. function pointAvoidli(e) {
  273. require(['modDir/tools/toolRouteSearch'], function (toolRouteSearch) {
  274. toolRouteSearch.setAvoidPoint(e.latlng);
  275. });
  276. };
  277. //到这里去
  278. function pointStopli(e) {
  279. require(['modDir/tools/toolRouteSearch'], function (toolRouteSearch) {
  280. toolRouteSearch.setStopPoint(e.latlng);
  281. });
  282. };
  283. //周边搜索
  284. function aroundFindli(e) {
  285. require(['modDir/tools/toolPlaceSearch'], function (toolPlaceSearch) {
  286. toolPlaceSearch.rightClickPopup(e.latlng);
  287. });
  288. };
  289. //加入地图书签
  290. // function addBookMarker() {
  291. //判断是否登录,没有登录进行提示
  292. // if (!ONEMAP.D.isLoad) {
  293. // ONEMAP.C.publisher.publish({
  294. // type: 'warning',
  295. // message: '请登录后使用!'
  296. // }, 'noteBar::add');
  297. // return false;
  298. // }
  299. // require(['modDir/user/userFav'], function (userFav) {
  300. // userFav.addFav();
  301. // });
  302. // };
  303. //鼠标抓手样式
  304. function removeMouseHand() {
  305. map2DViewer.map.off("mousedown", mouseDownStatus);
  306. map2DViewer.map.off("mouseup", mouseUpStatus);
  307. };
  308. function mouseDownStatus() {
  309. if (L.Browser.ie || L.Browser.firefox) {
  310. map2DViewer.map.getContainer().style.cursor = 'url(/images/layout/mouseDown.cur),auto';
  311. } else {
  312. map2DViewer.map.getContainer().style.cursor = 'url(/images/layout/mouseDown.cur) 5 5,auto';
  313. }
  314. }
  315. function mouseUpStatus() {
  316. if (L.Browser.ie || L.Browser.firefox) {
  317. map2DViewer.map.getContainer().style.cursor = 'url(/images/layout/mouseMove.cur),auto';
  318. } else {
  319. map2DViewer.map.getContainer().style.cursor = 'url(/images/layout/mouseMove.cur) 5 5,auto';
  320. }
  321. }
  322. function mouseHand() {
  323. mouseUpStatus();
  324. map2DViewer.map.on("mousedown", mouseDownStatus);
  325. map2DViewer.map.on("mouseup", mouseUpStatus);
  326. };
  327. //清空地图
  328. function cleanMapli() {
  329. ONEMAP.M.myLayerActions.clearLayer();
  330. ONEMAP.C.publisher.publish('', 'cleanMap');
  331. ONEMAP.C.publisher.publish({
  332. modName: 'cleanMap',
  333. }, 'tools:active');
  334. $("#cleanMap").click();
  335. };
  336. /**
  337. * 判断是否是正确的latlng
  338. * @param lat
  339. * @param lng
  340. * @returns {boolean}
  341. */
  342. function isNatureLatLng(lat, lng) {
  343. if (lat > -90 && lat < 90 && lng > -180 && lng < 180) {
  344. return true;
  345. } else {
  346. return false;
  347. }
  348. };
  349. function getContextmenuPlaceName(e) {
  350. contextmenuPlaceName.innerHTML = '正在搜索中...';
  351. var earthLatlng = L.Util.formatEarthLatLng(e.latlng);
  352. if (isNatureLatLng(earthLatlng.lat, earthLatlng.lng)) {
  353. var addressSearch = new addressSearchF();
  354. addressSearch.getAddressInfo({
  355. zoom: map2DViewer.map.getZoom(),
  356. latLng: [earthLatlng.lat, earthLatlng.lng]
  357. }, function (data) {
  358. data = data.data;
  359. contextmenuPlaceName.innerHTML = data.region ? data.region : '未知地点';
  360. });
  361. }
  362. };
  363. function getContextmenuLatlng(e) {
  364. modValue.latlng = e.latlng;
  365. var nLatlng = L.Util.formatHMS(e.latlng);
  366. contextmenuLatlng.innerHTML = nLatlng.lng + ' , ' + nLatlng.lat;
  367. };
  368. function getContextmenuCPJ(e) {
  369. // contextmenuCPJ.innerHTML = "当前磁偏角:正在查询中...";
  370. }
  371. function getCP(altData) {
  372. contextmenuCPJ.innerHTML = '正在查询中...';
  373. var ajaxUrl = onemapUrlConfig.declination;
  374. var newdata = new Date();
  375. var year = newdata.getFullYear();
  376. var month = (newdata.getMonth() + 1) < 10 ? '0' + (newdata.getMonth() + 1) : (newdata.getMonth() + 1);
  377. var day = newdata.getDate() < 10 ? '0' + newdata.getDate() : newdata.getDate();
  378. var ajaxData = {
  379. alt: altData,
  380. lat: modValue.latlng.lat,
  381. lon: modValue.latlng.lng,
  382. startYear: year,
  383. startMonth: month,
  384. startDay: day
  385. };
  386. //console.log(ajaxData);
  387. $.ajax({
  388. url: ajaxUrl + '?alt=' + altData + '&lat=' + ajaxData.lat + '&lon=' + ajaxData.lon + '&startYear=' + year + '&startMonth=' + month + '&startDay=' + day,
  389. type: 'GET',
  390. dataType: 'json',
  391. // data: ajaxData,
  392. })
  393. .done(function (data) {
  394. // //console.log(data);
  395. // //console.log(data.declination);
  396. contextmenuCPJ.innerHTML = "当前磁偏角:" + data.declination;
  397. //console.log("success");
  398. })
  399. .fail(function (data) {
  400. // //console.log(data);
  401. // //console.log("error");
  402. contextmenuCPJ.innerHTML = "当前磁偏角:暂无数据";
  403. })
  404. .always(function () {
  405. //console.log("complete");
  406. });
  407. }
  408. function getContextmenuDEMHeight(e) {
  409. contextmenuDEMHeight.innerHTML = '正在查询中...';
  410. var ajaxUrl = onemapUrlConfig.elevationDataUrl + '/v1.0/geoprocessing/single_elevation';
  411. var ajaxData = {
  412. lat: e.latlng.lat,
  413. lon: e.latlng.lng,
  414. zoom: map23DData.view.zoom,
  415. delta_zoom: 0
  416. };
  417. $.ajax({
  418. type: "get",
  419. dataType: 'json',
  420. data: ajaxData,
  421. url: ajaxUrl,
  422. beforeSend: ONEMAP.V.loading.load(),
  423. success: function (data) {
  424. if (data.code == 0) {
  425. contextmenuDEMHeight.innerHTML = "当前高程: " + data.data + "米";
  426. // getCP(data.data);
  427. } else {
  428. contextmenuDEMHeight.innerHTML = "当前高程: " + data.message;
  429. }
  430. },
  431. completer: ONEMAP.V.loading.loaded()
  432. });
  433. }
  434. /**
  435. * 获取当前点地形数据
  436. * [getTerrainanalysis description]
  437. * @return {[type]} [description]
  438. */
  439. function getTerrainanalysis(e) {
  440. contextmenuTerrainanalysis.innerHTML = '地形查询中...';
  441. var curlatlng = e.latlng.lat + "," + e.latlng.lng
  442. //获取该点坡向
  443. $.ajax({
  444. type: "get",
  445. dataType: 'json',
  446. data: {
  447. locations: curlatlng
  448. },
  449. url: onemapUrlConfig.elevationDataUrl + '/v2.0/geoprocessing/aspect',
  450. beforeSend: ONEMAP.V.loading.load(),
  451. success: function (data) {
  452. if (data.status == 'ok') {
  453. modValue.curPX = data.results[0].aspect.toFixed(3);
  454. contextmenuTerrainanalysis.innerHTML = "坡向: " + modValue.curPX;
  455. $.ajax({
  456. type: "get",
  457. dataType: 'json',
  458. data: {
  459. locations: curlatlng
  460. },
  461. url: onemapUrlConfig.elevationDataUrl + '/v2.0/geoprocessing/slope',
  462. beforeSend: ONEMAP.V.loading.load(),
  463. success: function (data) {
  464. if (data.status == 'ok') {
  465. modValue.PD = data.results[0].slope.toFixed(3);
  466. contextmenuTerrainanalysis.innerHTML = "坡向:" + modValue.curPX + ";坡度:" + modValue.PD;
  467. // getCP(data.data);
  468. } else {
  469. contextmenuTerrainanalysis.innerHTML = "坡向:无数据;坡度:无数据";
  470. }
  471. },
  472. completer: ONEMAP.V.loading.loaded()
  473. });
  474. // getCP(data.data);
  475. } else {
  476. contextmenuTerrainanalysis.innerHTML = "坡向: 无数据";
  477. }
  478. },
  479. completer: ONEMAP.V.loading.loaded()
  480. });
  481. }
  482. /**
  483. * 地图容器重置
  484. * @return {[type]} [description]
  485. */
  486. function mapResize() {
  487. if (ONEMAP.M.pcLayout.status.showFullMap) {
  488. $('#map3DWrap').css({
  489. height: $(window).height(),
  490. top: '0px'
  491. });
  492. } else {
  493. $('#map3DWrap').css({
  494. height: $(window).height(),
  495. top: '0px'
  496. });
  497. }
  498. setTimeout(function () {
  499. map2DViewer.map.invalidateSize();
  500. }, 100);
  501. }
  502. function resetMapCenterZoomLayer(center, zoom) {
  503. map2DViewer.setView({
  504. center: {
  505. lat: center.lat,
  506. lng: center.lon
  507. },
  508. zoom: zoom
  509. })
  510. }
  511. /**
  512. * 注册监听
  513. * @type {Function}
  514. */
  515. function subscribe() {
  516. ONEMAP.C.publisher.subscribe(setSTLatLngLayer, 'mapHolder::STLatLngLayer');
  517. ONEMAP.C.publisher.subscribe(mapResize, 'layout::resize');
  518. };
  519. return ONEMAP.M.mapHolder = {
  520. init: init,
  521. modValue: modValue,
  522. mouseHand: mouseHand,
  523. removeMouseHand: removeMouseHand,
  524. getContextmenuPlaceName: getContextmenuPlaceName,
  525. resetMapCenterZoomLayer: resetMapCenterZoomLayer
  526. };
  527. })