toolMapShotAndDownLoad.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /**
  2. * @fileoverview 工具 测量地图面积 模块
  3. * @author Song.Huang
  4. * @version 1.0.0
  5. */
  6. define(['vendorDir/map23dlib/leaflet.mapshot',
  7. 'css!vendorDir/map23dlib/leaflet.mapshot'
  8. ],
  9. function() {
  10. /**
  11. * 状态值
  12. * @type {Boolean}
  13. * @default false
  14. * @private
  15. */
  16. var status = {
  17. initialized: false //是否初始化
  18. }
  19. /**
  20. * 模块数据 用于数据存储和外部调用
  21. * @type {Object}
  22. * 数据存放
  23. */
  24. var modValue = {
  25. loopAjaxTimeOut: {},
  26. time_stamp: '',
  27. postData: null,
  28. mapShotControl: null,
  29. offsetX :[0,0,108000,72000,54000,27000,13500,10800,5400,2700,1350,675,450,225,135,90,45,30,20,10],
  30. offsetY :[0,0,72000,54000,36000,18000,9000,7200,3600,1800,900,450,300,150,90,60,30,20,10,5],
  31. BLC: [200000000, 100000000, 50000000, 25000000, 12000000, 6000000, 1600000, 1550000, 800000, 400000, 200000, 100000, 50000, 25000, 12000, 2300, 3000, 1500, 800],
  32. scaleMap:[591657527.6,295828763.8,147914381.9,73957190.95,36978595.47,18489297.74,9244648.868,4622324.434,2311162.217,1155581.109,577790.5543,288895.2771,144447.6386,72223.81928,36111.90964,18055.95482,9027.97741,4513.988705,2256.994353,1128.497176]
  33. }
  34. var status = {
  35. countingLock: false
  36. }
  37. /**
  38. * 初始化
  39. * 监听事件
  40. * @type {Function}
  41. */
  42. function init(type) {
  43. if (!status.initialized) {
  44. status.initialized = true;
  45. setLayout();
  46. bindEvent();
  47. subscribe();
  48. modValue.mapShotControl = L.control.mapShot().addTo(map2DViewer.map);
  49. }
  50. if (type == 2) {
  51. ONEMAP.C.publisher.publish({
  52. modName: 'msAll'
  53. }, 'tools:active');
  54. } else {
  55. ONEMAP.C.publisher.publish({
  56. modName: 'toolMapShot'
  57. }, 'tools:active');
  58. }
  59. }
  60. function setLayout() {
  61. $('body').append($('<div id="mapShotDownload"><p></p></div>'));
  62. $('body').append($('#toolsBar .zhengshiDl'));
  63. $(window).resize(function() {
  64. layoutResize();
  65. });
  66. };
  67. function bindEvent() {
  68. };
  69. function layoutResize() {
  70. };
  71. function mapfishPostData(postData) {
  72. var newPostData = encodeURIComponent(changeMapShotData(postData));
  73. // console.log(modValue.mapShotDrawData)
  74. //var appId = 'print_osm_new_york_EPSG_900913';
  75. var appId = "print_osm_EPSG3857";
  76. if($("#paperSize").val() == 4){
  77. appId = 'print_osm_EPSG3857';
  78. }else if($("#paperSize").val() == 3){
  79. appId = 'print_osm_EPSG3857_a3';
  80. }
  81. var format = postData.ext;
  82. //console.log(changeMapShotData(postData));
  83. var startTime = new Date().getTime();
  84. ONEMAP.C.publisher.publish({ type: 'success', message: '开始下载' }, 'noteBar::add');
  85. //return false;
  86. $.ajax({
  87. type: 'POST',
  88. url: onemapUrlConfig.mapFishServerUrl + '/print/' + appId + '/report.' + format,
  89. data: newPostData,
  90. success: function(data) {
  91. downloadWhenReady(startTime, data);
  92. },
  93. error: function(data) {
  94. ONEMAP.C.publisher.publish({ type: 'error', message: '下载出错' }, 'noteBar::add');
  95. },
  96. dataType: 'json'
  97. });
  98. };
  99. function downloadWhenReady(startTime, data) {
  100. if ((new Date().getTime() - startTime) > 30000) {
  101. ONEMAP.C.publisher.publish({ type: 'success', message: '准备下载' }, 'noteBar::add');
  102. } else {
  103. updateWaitingMsg(startTime, data);
  104. setTimeout(function() {
  105. $.getJSON(onemapUrlConfig.mapFishServerUrl + data.statusURL, function(statusData) {
  106. if (!statusData.done) {
  107. downloadWhenReady(startTime, data);
  108. } else {
  109. window.location = onemapUrlConfig.mapFishServerUrl + statusData.downloadURL;
  110. //ONEMAP.C.publisher.publish({ type: 'success', message: 'Downloading: ' + data.ref }, 'noteBar::add');
  111. }
  112. }, function error(data) { ONEMAP.C.publisher.publish({ type: 'error', message: '下载出错' }, 'noteBar::add') });
  113. }, 1000);
  114. }
  115. }
  116. function changeMapShotData(postData) {
  117. var scale = modValue.BLC[postData.zoom - 1];
  118. var mapcenter = lonlatToMercator(postData.center);
  119. var geojsonFeatures = [];
  120. var geojsonStyles = {};
  121. if ($(".zhengshiDl .stw").is(':checked')) {
  122. //计算截图中心点
  123. var ST = getPaperBounds(
  124. {
  125. lat:postData.center[1],
  126. lng:postData.center[0]
  127. },
  128. 297,
  129. 210,
  130. postData.zoom
  131. )
  132. //获取截图元素数据
  133. $.each(ST.polylineArry,function(i,t){
  134. var guid = map23DControl.buildGuid('mapfish_style');
  135. var polylinescoor = [];
  136. for (var i = 0; i < t.length; i++) {
  137. curpolylinescoor = stringToParse([t[i][1], t[i][0]]);
  138. curpolylinescoor = lonlatToMercator(curpolylinescoor);
  139. polylinescoor.push(curpolylinescoor);
  140. }
  141. var guid = guid;
  142. var fItem = {
  143. "geometry": {
  144. "coordinates": polylinescoor,
  145. "type": "LineString"
  146. },
  147. "properties": {
  148. "_gx_style": guid
  149. },
  150. "type": "Feature"
  151. };
  152. geojsonStyles[guid] = {
  153. "strokeColor": "#04cfed",
  154. "strokeDashstyle": "solid",
  155. "strokeLinecap": "round",
  156. "strokeOpacity": 1,
  157. "strokeWidth": 2
  158. };
  159. geojsonFeatures.push(fItem);
  160. })
  161. $.each(ST.markerArry,function(i,t){
  162. var guid = map23DControl.buildGuid('mapfish_style');
  163. var curmarkerCoor = stringToParse([t.latlng[1], t.latlng[0]]);
  164. curmarkerCoor = lonlatToMercator(curmarkerCoor)
  165. var fItem = {
  166. "geometry": {
  167. "coordinates": curmarkerCoor,
  168. "type": "Point"
  169. },
  170. "properties": {
  171. "_gx_style": guid
  172. },
  173. "type": "Feature"
  174. };
  175. geojsonStyles[guid] = {
  176. "fontColor": "#ff6600",
  177. "fontSize": "40px",
  178. "fontStyle": "normal",
  179. "fontWeight": "normal",
  180. "label": t.title,
  181. "labelAlign": "cm",
  182. "labelXOffset": t.offset[0],
  183. "labelYOffset": t.offset[1],
  184. };
  185. geojsonFeatures.push(fItem);
  186. })
  187. }
  188. $.each(map2DViewer.map._layers, function(i, t) {
  189. var guid = map23DControl.buildGuid('mapfish_style');
  190. if (t.shape == 'Marker') {
  191. var curmarkerCoor = stringToParse([t._latlng.lng, t._latlng.lat]);
  192. curmarkerCoor = lonlatToMercator(curmarkerCoor)
  193. var fItem = {
  194. "geometry": {
  195. "coordinates": curmarkerCoor,
  196. "type": "Point"
  197. },
  198. "properties": {
  199. "_gx_style": guid
  200. },
  201. "type": "Feature"
  202. };
  203. var iconUrl = onemapUrlConfig.siteUrl + '/scripts/vendor/leaflet/draw_new/images/marker-icon.png';
  204. if (t.options.icon) {
  205. iconUrl = t.options.icon.options.iconUrl;
  206. if (iconUrl.length < 18) {
  207. iconUrl = onemapUrlConfig.siteUrl + '/scripts/vendor/leaflet/draw_new/images/marker-icon.png';
  208. }
  209. geojsonStyles[guid] = {
  210. "fillColor": "#ff0000",
  211. "fillOpacity": 1,
  212. "pointRadius": 25,
  213. "externalGraphic": iconUrl,
  214. "graphicOpacity": 1,
  215. "graphicFormat": "image/png"
  216. };
  217. geojsonFeatures.push(fItem);
  218. }
  219. if (t.zhuji) {
  220. var zguid = map23DControl.buildGuid('mapfish_style');
  221. var zfItem = {
  222. "geometry": {
  223. "coordinates": curmarkerCoor,
  224. "type": "Point"
  225. },
  226. "properties": {
  227. "_gx_style": zguid
  228. },
  229. "type": "Feature"
  230. };
  231. geojsonStyles[zguid] = {
  232. "fontColor": "#f00",
  233. "fontFamily": "黑体",
  234. "fontSize": "20px",
  235. "fontStyle": "normal",
  236. "fontWeight": "normal",
  237. "label": t.zhuji,
  238. "labelAlign": "cm",
  239. "labelXOffset": "60.0",
  240. "labelYOffset": "65.0",
  241. };
  242. geojsonFeatures.push(zfItem);
  243. }
  244. } else if (t.shape == 'Line') {
  245. var polylinescoor = [];
  246. for (var i = 0; i < t._latlngs.length; i++) {
  247. curpolylinescoor = stringToParse([t._latlngs[i].lng, t._latlngs[i].lat]);
  248. curpolylinescoor = lonlatToMercator(curpolylinescoor);
  249. polylinescoor.push(curpolylinescoor);
  250. }
  251. var guid = guid;
  252. var fItem = {
  253. "geometry": {
  254. "coordinates": polylinescoor,
  255. "type": "LineString"
  256. },
  257. "properties": {
  258. "_gx_style": guid
  259. },
  260. "type": "Feature"
  261. };
  262. geojsonStyles[guid] = {
  263. "strokeColor": t.options.color,
  264. "strokeDashstyle": "solid",
  265. "strokeLinecap": "round",
  266. "strokeOpacity": 1,
  267. "strokeWidth": 2
  268. };
  269. geojsonFeatures.push(fItem);
  270. } else if (t.shape == 'Poly' || t.shape == 'Rectangle') {
  271. var polygonscoor = [];
  272. polygonscoor[0] = [];
  273. for (var i = 0; i <= t._latlngs[0].length; i++) {
  274. if (i < t._latlngs[0].length) {
  275. var curpolygonscoor = stringToParse([t._latlngs[0][i].lng, t._latlngs[0][i].lat]);
  276. curpolygonscoor = lonlatToMercator(curpolygonscoor);
  277. polygonscoor[0].push(curpolygonscoor);
  278. } else {
  279. var curpolygonscoor = stringToParse([t._latlngs[0][0].lng, t._latlngs[0][0].lat]);
  280. curpolygonscoor = lonlatToMercator(curpolygonscoor);
  281. polygonscoor[0].push(curpolygonscoor);
  282. }
  283. }
  284. var fItem = {
  285. "geometry": {
  286. "coordinates": polygonscoor,
  287. "type": "Polygon"
  288. },
  289. "properties": {
  290. "_gx_style": guid
  291. },
  292. "type": "Feature"
  293. };
  294. geojsonStyles[guid] = {
  295. "fillColor": t.options.color,
  296. "fillOpacity": 0.5,
  297. "strokeColor": t.options.color,
  298. "strokeDashstyle": "solid",
  299. "strokeLinecap": "round",
  300. "strokeOpacity": 1,
  301. "strokeWidth": 2
  302. };
  303. geojsonFeatures.push(fItem);
  304. } else if (t.shape == 'Circle') {
  305. //圆心
  306. var allCirclePoint = [];
  307. var center_lat = t._latlng.lat;
  308. var center_lon = t._latlng.lng;
  309. //半径
  310. var radius = t.options.radius; //米
  311. //转换定义 核心
  312. var epsg_4326 = "+proj=longlat +datum=WGS84 +no_defs "
  313. var esri_102016 = "+proj=aeqd +lat_0=90 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m no_defs"
  314. var self_defined = "+proj=aeqd +lat_0=" + center_lat + "+lon_0=" + center_lon + "+x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m no_defs"
  315. //生成圆平面点;
  316. var pointArr = L.Util.getCirclePoint(0, 0, radius);
  317. //圆的轮廓点转换为经纬度,当做GeoJSON来使用。
  318. var circlePoints = [];
  319. for (var k = 0; k < pointArr.length; k++) {
  320. var item = pointArr[k];
  321. //核心 将平面坐标转经纬度坐标
  322. var xy = proj4(self_defined, epsg_4326, [item.x, item.y]);
  323. circlePoints.push([
  324. xy[0],
  325. xy[1]
  326. ])
  327. }
  328. var polygonscoor = [];
  329. polygonscoor[0] = [];
  330. for (var i = 0; i < circlePoints.length + 1; i++) {
  331. if (i < circlePoints.length) {
  332. var curpolygonscoor = stringToParse([circlePoints[i][0], circlePoints[i][1]]);
  333. curpolygonscoor = lonlatToMercator(curpolygonscoor);
  334. polygonscoor[0].push(curpolygonscoor);
  335. } else {
  336. var curpolygonscoor = stringToParse([circlePoints[0][0], circlePoints[0][1]]);
  337. curpolygonscoor = lonlatToMercator(curpolygonscoor);
  338. polygonscoor[0].push(curpolygonscoor);
  339. }
  340. }
  341. var fItem = {
  342. "geometry": {
  343. "coordinates": polygonscoor,
  344. "type": "Polygon"
  345. },
  346. "properties": {
  347. "_gx_style": guid
  348. },
  349. "type": "Feature"
  350. };
  351. geojsonStyles[guid] = {
  352. "fillColor": t.options.color,
  353. "fillOpacity": 0.5,
  354. "strokeColor": t.options.color,
  355. "strokeDashstyle": "solid",
  356. "strokeLinecap": "round",
  357. "strokeOpacity": 1,
  358. "strokeWidth": 2
  359. };
  360. geojsonFeatures.push(fItem);
  361. } else if (t.shape == 'Label') {
  362. var curmarkerCoor = stringToParse([t._latlng.lng, t._latlng.lat]);
  363. curmarkerCoor = lonlatToMercator(curmarkerCoor)
  364. var fItem = {
  365. "geometry": {
  366. "coordinates": curmarkerCoor,
  367. "type": "Point"
  368. },
  369. "properties": {
  370. "_gx_style": guid
  371. },
  372. "type": "Feature"
  373. };
  374. if (t.options.icon) {
  375. var cl = t.options.icon.options.className;
  376. var scolor = $(".leaflet-marker-icon." + cl).css("color");
  377. var slb = $(".leaflet-marker-icon." + cl).text();
  378. var sfont = $(".leaflet-marker-icon." + cl).css("font-family");
  379. if (scolor != undefined) {
  380. geojsonStyles[guid] = {
  381. "fontColor": scolor,
  382. "fontFamily": sfont || 'microsoft yahei',
  383. "fontSize": t.options.icon.options.iconSize + "px",
  384. "fontStyle": "normal",
  385. "fontWeight": "normal",
  386. "label": t.zhuji || slb,
  387. "labelAlign": "cm",
  388. "labelXOffset": "-25.0",
  389. "labelYOffset": "-35.0",
  390. };
  391. console.log(guid)
  392. console.log(geojsonStyles[guid])
  393. geojsonFeatures.push(fItem);
  394. }
  395. } else {
  396. geojsonStyles[guid] = {
  397. "fontColor": t.options.Oc,
  398. "fontFamily": t.options.Off,
  399. "fontSize": t.options.Ofz + "px",
  400. "fontStyle": "normal",
  401. "fontWeight": "normal",
  402. "label": t.options.txt,
  403. "labelAlign": "cm",
  404. "labelXOffset": "-25.0",
  405. "labelYOffset": "-35.0",
  406. };
  407. geojsonFeatures.push(fItem);
  408. }
  409. }
  410. })
  411. geojsonStyles.styleProperty = "_gx_style";
  412. geojsonStyles.version = 1;
  413. var curpostData = {
  414. "outputFormat": "png",
  415. "attributes": {
  416. "description": postData.mapshotcopyright,
  417. //"northArrowDef": nad,
  418. "map": {
  419. "center": mapcenter,
  420. "dpi": 72,
  421. "layers": [{
  422. "geoJson": {
  423. "features": geojsonFeatures,
  424. "type": "FeatureCollection"
  425. },
  426. "style": geojsonStyles,
  427. "type": "geojson"
  428. }, {
  429. // "baseURL": map23DConfig.tileServerUrl + "/" + postData.guid + "?l={z}&x={x}&y={y}",
  430. "baseURL": map23DConfig.tileServerFo3DUrl + "/" + postData.guid + "?l={z}&x={x}&y={y}",
  431. "imageExtension": "png",
  432. "type": "OSM"
  433. }],
  434. "projection": "EPSG:3857",
  435. "rotation": 0,
  436. "scale": scale,
  437. "useAdjustBounds": true
  438. // "height":20
  439. },
  440. "title": postData.mapshotName,
  441. //"scalebar": scb
  442. },
  443. "layout": "A4 landscape"
  444. }
  445. if (!$(".zhengshiDl .blc").is(':checked')) {
  446. curpostData.attributes.scalebar = {
  447. "barSize": 0,
  448. "padding": 0,
  449. "backgroundColor": "rgba(255,255,255,0.001)",
  450. "barBgColor": "rgba(255,255,255,0.001)",
  451. "color": "rgba(255,255,255,0.001)",
  452. "fontColor": "rgba(255,255,255,0.001)"
  453. }
  454. }
  455. if (!$(".zhengshiDl .zbz").is(':checked')) {
  456. curpostData.attributes.northArrowDef = {
  457. "backgroundColor": "rgba(255,255,255,0.001)",
  458. "graphic": onemapUrlConfig.siteUrl + "/images/layout/0.png"
  459. }
  460. }
  461. return postData = JSON.stringify(curpostData);
  462. };
  463. function updateWaitingMsg(startTime, data) {
  464. var elapsed = Math.floor((new Date().getTime() - startTime) / 100);
  465. var time = '';
  466. if (elapsed > 5) {
  467. time = (elapsed / 10) + " sec";
  468. }
  469. ONEMAP.C.publisher.publish({ type: 'success', message: '等候时间' + time }, 'noteBar::add');
  470. }
  471. //经纬度转魔卡托
  472. function lonlatToMercator(lonlat) {
  473. var mercator = [];
  474. var x = lonlat[0] * 20037508.34 / 180;
  475. var y = Math.log(Math.tan((90 + lonlat[1]) * Math.PI / 360)) / (Math.PI / 180);
  476. y = y * 20037508.34 / 180;
  477. mercator.push(x);
  478. mercator.push(y);
  479. return mercator;
  480. }
  481. function stringToParse(lnglat) {
  482. var x = JSON.parse(lnglat[0]);
  483. var y = JSON.parse(lnglat[1]);
  484. var parsecoor = [x, y];
  485. return parsecoor;
  486. }
  487. function mapShotPostData(postData) {
  488. modValue.postData = postData;
  489. var LTpx = map2DViewer.map.project(
  490. new L.LatLng(JSON.parse(postData.bbox)[1], JSON.parse(postData.bbox)[0]),
  491. postData.zoom
  492. );
  493. var RBpx = map2DViewer.map.project(
  494. new L.LatLng(JSON.parse(postData.bbox)[3], JSON.parse(postData.bbox)[2]),
  495. postData.zoom
  496. );
  497. var countDownTF = parseInt(((RBpx.x - LTpx.x) * (RBpx.y - LTpx.y)) / (5120 * 5120));
  498. if (countDownTF > onemapUrlConfig.mapShotMaxPicCount) {
  499. var xiangsu = parseInt(5120 * Math.sqrt(onemapUrlConfig.mapShotMaxPicCount));
  500. $('#mapShotDownload p').html('地图拼接下载像素超过了' + xiangsu + '×' + xiangsu + '像素,请缩小截图下载区域<br/>' +
  501. '<span id="continueMapShot" onclick="ONEMAP.M.toolMapShot.continueMapShot()">修改区域</span>')
  502. $('#mapShotDownload').show();
  503. return false;
  504. }
  505. $('#mapShotDownload').show();
  506. var nDate = new Date();
  507. modValue.time_stamp = nDate.getTime();
  508. var url = onemapUrlConfig.mapShotDownServerUrl + '/puzzles/?bbox=' + postData.bbox + '&zoom=' + postData.zoom + '&type=' + postData.guid + '&ext=' + postData.ext + '&time_stamp=' + modValue.time_stamp + '&name=' + encodeURI(postData.name);
  509. $.ajax({
  510. type: "get",
  511. url: url,
  512. dataType: 'jsonp',
  513. jsonp: 'callback',
  514. success: function(data) {
  515. //modValue._loopGetResult({ajaxKey:'1'});
  516. }
  517. });
  518. setTimeout(function() {
  519. loopGetResult({ ajaxKey: modValue.time_stamp });
  520. }, 1000);
  521. };
  522. function againMapShotPostData() {
  523. mapShotPostData(modValue.postData);
  524. }
  525. function loopGetResult(options) {
  526. $.ajax({
  527. url: onemapUrlConfig.mapShotDownServerUrl + '/processing/' + modValue.time_stamp,
  528. type: "get",
  529. dataType: 'jsonp',
  530. jsonp: 'callback',
  531. success: function(data) {
  532. if (data.processing == '') {
  533. $('#mapShotDownload p').html('服务器处理失败,请重试! <br/><span id="continueMapShot" onclick="ONEMAP.M.toolMapShot.againMapShot()">再次提交</span>');
  534. }
  535. if (data.processing.length < 3) {
  536. if (modValue.loopAjaxTimeOut.hasOwnProperty(options.ajaxKey)) {
  537. clearTimeout(modValue.loopAjaxTimeOut[options.ajaxKey]);
  538. }
  539. $('#mapShotDownload p').html('服务器正在截图,截图进度 ' + data.processing + ' %. <br/><span id="continueMapShot" onclick="ONEMAP.M.toolMapShot.continueMapShot()">取消截图</span>');
  540. modValue.loopAjaxTimeOut[options.ajaxKey] = setTimeout(function() {
  541. loopGetResult(options);
  542. }, 1000);
  543. } else if (data.processing == '100') {
  544. if (modValue.loopAjaxTimeOut.hasOwnProperty(options.ajaxKey)) {
  545. clearTimeout(modValue.loopAjaxTimeOut[options.ajaxKey]);
  546. }
  547. $('#mapShotDownload p').html('服务器已完成截图,正在进行打包. <br/><span id="continueMapShot" onclick="ONEMAP.M.toolMapShot.continueMapShot()">取消截图</span>');
  548. modValue.loopAjaxTimeOut[options.ajaxKey] = setTimeout(function() {
  549. loopGetResult(options);
  550. }, 1000);
  551. } else {
  552. delete modValue.loopAjaxTimeOut[options.ajaxKey];
  553. //获取最终结果
  554. $('#mapShotDownload p').html('服务器截图完成,<a target="_blank" href="' + onemapUrlConfig.mapShotDownServerUrl + '/image_zip/' + data.processing + '">点击下载!</a><br/><span id="continueMapShot" onclick="ONEMAP.M.toolMapShot.continueMapShot()">继续截图</span><span style="cursor:pointer" onclick="ONEMAP.M.toolMapShot.exitMapShot();">退出截图</span>')
  555. //alert(data);
  556. }
  557. }
  558. });
  559. };
  560. function continueMapShot() {
  561. if (modValue.loopAjaxTimeOut.hasOwnProperty(modValue.time_stamp)) {
  562. clearTimeout(modValue.loopAjaxTimeOut[modValue.time_stamp]);
  563. }
  564. $('#mapShotDownload').hide().html('<p>正在提交截图数据到服务器</p>');
  565. }
  566. function exitMapShot() {
  567. $('#mapshotScreen').click();
  568. modValue.mapShotControl.remove();
  569. $('#mapShotDownload').hide()
  570. }
  571. function mapChange() {
  572. var centerlng = (map2DViewer.map.getCenter().lng).toFixed(5);
  573. var centerlat = (map2DViewer.map.getCenter().lat).toFixed(5);
  574. $(".zhengshiDl #mapfishCenter").html("纬度:" + centerlat + ",经度:" + centerlng);
  575. }
  576. /**
  577. * [getPaperBounds description]
  578. * 根据中心点经纬度和纸张大小获取纸张经纬度范围
  579. * @return {[type]} [description]
  580. */
  581. function getPaperBounds(center, width, height,zoom) {
  582. //中心点经纬度转墨卡托
  583. var centerMercator = L.Util.transformMercator({
  584. x: center.lng,
  585. y: center.lat
  586. })
  587. //根据纸张大小和所选比例尺等级确定墨卡托范围
  588. var scale = modValue.scaleMap[zoom-1];
  589. var scaleWidth = scale * width /1000 / 2;
  590. var scaleHeight = scale * height /1000 / 2;
  591. var NElatlng = {
  592. X: centerMercator.x + scaleWidth,
  593. Y: centerMercator.y + scaleHeight
  594. }
  595. var SWlatlng = {
  596. X: centerMercator.x - scaleWidth,
  597. Y: centerMercator.y - scaleHeight
  598. }
  599. NElatlng = L.Util.formMercatorToLatlng(NElatlng);
  600. SWlatlng = L.Util.formMercatorToLatlng(SWlatlng);
  601. return showSTLayer({
  602. zoom:zoom,
  603. bounds:{
  604. _northEast:{
  605. lat:NElatlng.Y,
  606. lng:NElatlng.X
  607. },
  608. _southWest:{
  609. lat:SWlatlng.Y,
  610. lng:SWlatlng.X
  611. }
  612. }
  613. })
  614. }
  615. function showSTLayer(options) {
  616. var offsetX = modValue.offsetX[options.zoom];
  617. var offsetY = modValue.offsetY[options.zoom];
  618. var startSW = {
  619. lat: (parseInt(options.bounds._southWest.lat * 3600 / offsetY) - 1) * offsetY,
  620. lng: (parseInt(options.bounds._southWest.lng * 3600 / offsetX) - 1) * offsetX
  621. };
  622. var startNE = {
  623. lat: (parseInt(options.bounds._northEast.lat * 3600 / offsetY) + 1) * offsetY,
  624. lng: (parseInt(options.bounds._northEast.lng * 3600 / offsetX) + 1) * offsetX
  625. };
  626. var polylineArry = [];
  627. var markerArry = [];
  628. //todo 这里需要做下优化 优化内容: 经纬度坐标文字更新和画线更新分离开来,让地图移动的时候让文字始终靠在界面边上
  629. for (var i = startSW.lat; i < startNE.lat; i += offsetY) {
  630. polylineArry.push([
  631. [i / 3600, startSW.lng / 3600],
  632. [i / 3600, startNE.lng / 3600]
  633. ])
  634. //左
  635. if (L.Util.verifyLatLng(i / 3600, options.bounds._southWest.lng)) {
  636. markerArry.push({
  637. latlng:[i / 3600, options.bounds._southWest.lng],
  638. title:formatHMS([i, options.bounds._southWest.lng*3600]).lat,
  639. offset:[60,0]
  640. })
  641. }
  642. //右
  643. if (L.Util.verifyLatLng(i / 3600, options.bounds._northEast.lng)) {
  644. markerArry.push({
  645. latlng:[i / 3600, options.bounds._northEast.lng],
  646. title:formatHMS([i, options.bounds._northEast.lng*3600]).lat,
  647. offset:[-10,0]
  648. })
  649. }
  650. }
  651. for (var ii = startSW.lng; ii < startNE.lng; ii += offsetX) {
  652. polylineArry.push([
  653. [startSW.lat / 3600, ii / 3600],
  654. [startNE.lat / 3600, ii / 3600]
  655. ])
  656. if (L.Util.verifyLatLng(options.bounds._southWest.lat, ii / 3600)) {
  657. markerArry.push({
  658. latlng:[options.bounds._southWest.lat, ii / 3600],
  659. title:formatHMS([options.bounds._southWest.lng*3600, ii]).lng,
  660. offset:[0,-60]
  661. })
  662. }
  663. if (L.Util.verifyLatLng(options.bounds._northEast.lat, ii / 3600)) {
  664. markerArry.push({
  665. latlng:[options.bounds._northEast.lat, ii / 3600],
  666. title:formatHMS([options.bounds._northEast.lng*3600, ii]).lng,
  667. offset:[0,90]
  668. })
  669. }
  670. }
  671. return {
  672. polylineArry:polylineArry,
  673. markerArry:markerArry
  674. }
  675. }
  676. /**
  677. * 分格式化
  678. * @param {[type]} latlng [分]
  679. * @return {[type]} [description]
  680. */
  681. function formatHMS(latlng){
  682. var lat = latlng.hasOwnProperty('lat')?latlng.lat:latlng[0];
  683. var lng = latlng.hasOwnProperty('lng')?latlng.lng:latlng[1];
  684. function setHMS(f){
  685. var h = parseInt(f/3600);
  686. var m = parseInt((f-h*3600)/60);
  687. var s = parseInt((f-h*3600-m*60));
  688. if(m.toString().length == 1){
  689. m = '0' + m.toString();
  690. }
  691. if(s.toString().length == 1){
  692. s = '0' + s.toString();
  693. }
  694. return h + '\u00b0' + m + '\u2032' + s + '\u2033';
  695. }
  696. var nLat = '';
  697. var nLng = '';
  698. if (lat < 0) {
  699. nLat = setHMS(lat * -1)+'S';
  700. } else {
  701. nLat = setHMS(lat)+'N';
  702. }
  703. if (lng < 0) {
  704. nLng = setHMS(lng * -1)+'W';
  705. } else {
  706. nLng = setHMS(lng)+'E';
  707. }
  708. return {lat:nLat,lng:nLng};
  709. };
  710. /**
  711. * 注册监听
  712. */
  713. function subscribe() {
  714. ONEMAP.C.publisher.subscribe(remove, 'tools:active');
  715. ONEMAP.C.publisher.subscribe(mapShotPostData, 'mapShot:postData');
  716. ONEMAP.C.publisher.subscribe(mapfishPostData, 'mapFish:postData');
  717. ONEMAP.C.publisher.subscribe(mapChange, 'mapChange');
  718. };
  719. function mapfishInit() {
  720. var baseMapObj = {
  721. translate: null,
  722. guid: null
  723. }
  724. baseMapObj.guid = ONEMAP.M.mapHolder.modValue.mainLayers;
  725. if (baseMapObj.guid === 'gm') {
  726. baseMapObj.translate = '交通图';
  727. baseMapObj.guid = 'gm';
  728. }
  729. if (baseMapObj.guid === 'gr') {
  730. baseMapObj.translate = '影像图';
  731. baseMapObj.guid = 'gr';
  732. }
  733. if (baseMapObj.guid === 'gr') {
  734. baseMapObj.translate = '影像叠加地名图';
  735. baseMapObj.guid = 'gr';
  736. }
  737. if (baseMapObj.guid === 'gt') {
  738. baseMapObj.translate = '地势图';
  739. baseMapObj.guid = 'gt';
  740. }
  741. $(".zhengshiDl").addClass('active');
  742. $(".zhengshiDl .name").attr("placeholder", baseMapObj.translate)
  743. $(".zhengshiDl .bili").val(map2DViewer.map.getZoom());
  744. var centerlng = (map2DViewer.map.getCenter().lng).toFixed(5);
  745. var centerlat = (map2DViewer.map.getCenter().lat).toFixed(5);
  746. $(".zhengshiDl #mapfishCenter").html("纬度:" + centerlat + ",经度:" + centerlng);
  747. $(".zhengshiDl .subdiv button").off("click").on("click", function() {
  748. postScreenMapFishInfo(baseMapObj);
  749. })
  750. }
  751. function postScreenMapFishInfo(curLayerObj) {
  752. var centerlng = (map2DViewer.map.getCenter().lng).toFixed(5) / 1;
  753. var centerlat = (map2DViewer.map.getCenter().lat).toFixed(5) / 1;
  754. newdata = new Date();
  755. year = newdata.getFullYear();
  756. month = newdata.getMonth() + 1;
  757. day = newdata.getDate().toString();
  758. var hour = newdata.getHours().toString();
  759. var mintes = newdata.getMinutes().toString();
  760. var second = newdata.getSeconds().toString();
  761. if (month < 10) {
  762. // month = "0" + month;
  763. }
  764. if (day < 10) {
  765. day = "0" + day;
  766. }
  767. if (hour < 10) {
  768. hour = "0" + hour;
  769. }
  770. if (mintes < 10) {
  771. mintes = "0" + mintes;
  772. }
  773. if (second < 10) {
  774. second = "0" + second;
  775. }
  776. var time = year + "年" + month + "月" + day + "日 " + hour + ":" + mintes + ":" + second;
  777. if (curLayerObj.guid == "gr") {
  778. curLayerObj.guid = 'gh';
  779. }
  780. var svname = $(".zhengshiDl .name").val();
  781. if (svname == "") {
  782. svname = curLayerObj.translate;
  783. }
  784. var svzoom = $('.zhengshiDl .bili option:selected').val();
  785. var svimgtype = $(".zhengshiDl .geshi option:selected").val();
  786. // $(_this.selectZoom).val()
  787. var postDataMapfish = {
  788. center: [centerlng, centerlat],
  789. name: svname,
  790. zoom: svzoom, //_this.selectZoom.value
  791. guid: curLayerObj.guid,
  792. ext: svimgtype, //'png'
  793. mapshotName: svname, //_this.curLayerObj.translate
  794. mapshotcopyright: time
  795. }
  796. ONEMAP.C.publisher.publish(postDataMapfish, 'mapFish:postData');
  797. }
  798. /**
  799. * 功能移除
  800. */
  801. function remove(options) {
  802. if (options.modName == 'toolMapShot') {
  803. if ($('.tools-mapshot').hasClass('cur')) {
  804. $('.tools-mapshot').removeClass('cur');
  805. modValue.mapShotControl.remove();
  806. } else {
  807. $('.tools-mapshot').addClass('cur');
  808. $('.tools-msAll').removeClass('cur');
  809. $(".zhengshiDl").removeClass('active')
  810. modValue.mapShotControl.init();
  811. }
  812. } else if (options.modName == 'msAll') {
  813. if ($('.tools-msAll').hasClass('cur')) {
  814. $('.tools-msAll').removeClass('cur');
  815. $(".zhengshiDl").removeClass('active');
  816. modValue.mapShotControl.remove();
  817. } else {
  818. $('.tools-msAll').addClass('cur');
  819. $('.tools-mapshot').removeClass('cur');
  820. $('#mapHolder .leaflet-mapshot-info').hide()
  821. modValue.mapShotControl.remove();
  822. mapfishInit();
  823. }
  824. } else {
  825. $('.tools-mapshot').removeClass('cur')
  826. $('.tools-msAll').removeClass('cur');
  827. $(".zhengshiDl").removeClass('active')
  828. modValue.mapShotControl.remove();
  829. }
  830. };
  831. return ONEMAP.M.toolMapShot = {
  832. init: init,
  833. continueMapShot: continueMapShot,
  834. exitMapShot: exitMapShot
  835. }
  836. });