leaflet.mapshot.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. L.Control.MapShot = L.Control.extend({
  2. options: {
  3. position: 'topright',
  4. },
  5. /**
  6. * 地图实例
  7. * @type {Object}
  8. * @private
  9. */
  10. _map: null,
  11. /**
  12. * 底图容器
  13. * @type {L}
  14. */
  15. _shotGroup: new L.FeatureGroup(),
  16. /**
  17. * 是否正在底图
  18. * @type {Boolean}
  19. * @default false
  20. * @private
  21. */
  22. _isScreenShot: false,
  23. _msType: 1,
  24. initialize: function(options) {
  25. L.setOptions(this, options);
  26. return this;
  27. },
  28. onAdd: function(map) {
  29. this._map = map;
  30. this._map.addLayer(this._shotGroup);
  31. this.rectangle = new L.Draw.Rectangle(this._map);
  32. this._createControl();
  33. return this._container;
  34. },
  35. _createControl: function() {
  36. var _this = this;
  37. var className = 'leaflet-control-mapshot',
  38. classNames = className + ' leaflet-bar leaflet-control';
  39. this._container = L.DomUtil.create('div', classNames);
  40. var link = L.DomUtil.create('a', 'leaflet-mapshot-link', this._container);
  41. var span = L.DomUtil.create('span', '', link);
  42. span.innerHTML = '底图';
  43. L.DomEvent.on(this._container, 'mousedown', L.DomEvent.stopPropagation)
  44. .on(this._container, 'dblclick', L.DomEvent.stopPropagation)
  45. .on(this._container, 'click', L.DomEvent.stopPropagation)
  46. .on(this._container, 'mousewheel', L.DomEvent.stopPropagation)
  47. .on(this._container, 'contextmenu', L.DomEvent.stopPropagation);
  48. //绑定事件
  49. L.DomEvent
  50. .on(link, 'click', L.DomEvent.stopPropagation)
  51. .on(link, 'click', L.DomEvent.preventDefault)
  52. .on(link, 'click', function() {
  53. if (_this._isScreenShot) {
  54. _this.remove();
  55. } else {
  56. _this.init();
  57. }
  58. });
  59. _this.mapShotInfoContainer = L.DomUtil.create('div', 'leaflet-mapshot-info', this._container);
  60. var title = L.DomUtil.create('h3', '', _this.mapShotInfoContainer);
  61. title.innerHTML = '底图信息';
  62. var labelLT = L.DomUtil.create('label', '', _this.mapShotInfoContainer);
  63. labelLT.innerHTML = '左上角坐标:';
  64. _this.pLT = L.DomUtil.create('p', '', _this.mapShotInfoContainer);
  65. var labelRB = L.DomUtil.create('label', '', _this.mapShotInfoContainer);
  66. labelRB.innerHTML = '右下角坐标:';
  67. _this.pRB = L.DomUtil.create('p', '', _this.mapShotInfoContainer);
  68. _this.labelTypeL = L.DomUtil.create('label', '', _this.mapShotInfoContainer);
  69. _this.labelTypeL.innerHTML = '图种名称:';
  70. _this.labelTypeP = L.DomUtil.create('p', '', _this.mapShotInfoContainer);
  71. _this.labelThematic = L.DomUtil.create('label', '', _this.mapShotInfoContainer);
  72. _this.labelThematic.innerHTML = '专题图名称:';
  73. _this.selectThematicType = L.DomUtil.create('select', '', _this.mapShotInfoContainer);
  74. $(_this.labelThematic).hide();
  75. $(_this.selectThematicType).hide();
  76. $(_this.selectThematicType).css("width", "100px");
  77. var labelZoom = L.DomUtil.create('label', '', _this.mapShotInfoContainer);
  78. labelZoom.innerHTML = '缩放级别:';
  79. _this.selectZoom = L.DomUtil.create('select', '', _this.mapShotInfoContainer);
  80. var labelPicType = L.DomUtil.create('label', '', _this.mapShotInfoContainer);
  81. labelPicType.innerHTML = '图片格式:';
  82. _this.selectPicType = L.DomUtil.create('select', '', _this.mapShotInfoContainer);
  83. var picType = ['png','jpg'];
  84. for (var ii = 0; ii < picType.length; ii++) {
  85. var item = new Option(picType[ii], picType[ii]);
  86. _this.selectPicType.options.add(item);
  87. }
  88. var subDiv = L.DomUtil.create('div', 'subdiv', _this.mapShotInfoContainer);
  89. _this.subBtn = L.DomUtil.create('button', 'maptile', subDiv);
  90. _this.subBtn.innerHTML = '底图下载';
  91. // _this.ZSsubBtn = L.DomUtil.create('button', 'wview', subDiv);
  92. // _this.ZSsubBtn.innerHTML = '整饰下载';
  93. _this.downDiv = L.DomUtil.create('div', 'download-panel', _this.mapShotInfoContainer);
  94. _this.downLink = L.DomUtil.create('p', 'download-link', _this.downDiv);
  95. L.DomEvent.on(_this.subBtn, 'click', function() {
  96. _this._postScreenShotInfo();
  97. });
  98. // L.DomEvent.on(_this.ZSsubBtn, 'click', function() {
  99. // _this._postScreenMapFishInfo();
  100. // });
  101. ONEMAP.C.publisher.subscribe(createC, 'tools:active');
  102. function createC(options){
  103. if(options.modName == 'msAll'){
  104. _this._msType = 2;
  105. } else{
  106. _this._msType = 1;
  107. }
  108. }
  109. },
  110. /**
  111. * 初始化
  112. * @type {Function}
  113. */
  114. init: function() {
  115. var _this = this;
  116. _this.rectangle.enable();
  117. _this._shotGroup.clearLayers();
  118. _this._isScreenShot = true;
  119. L.DomUtil.addClass(_this._container, 'active');
  120. _this._map.on('draw:created', _this.drawCreated, this);
  121. _this._map.on('singleLayer:addToMap', _this.updatePanel, this);
  122. _this._map.on('singleLayer:removeFromMap', _this.getBaseMap, this);
  123. _this._map.on('mainLayer:addLayer', _this.getBaseMap, this);
  124. _this._map.on('overLayer:addLayer', _this.getBaseMap, this);
  125. },
  126. layoutResize: function() {
  127. var curWindowHeigth = $(window).height();
  128. /*if(curWindowHeigth < 955){
  129. $("#mapHolder .leaflet-mapshot-info").css({
  130. "left":-130
  131. })
  132. }else{
  133. $("#mapHolder .leaflet-mapshot-info").css({
  134. "left":-179
  135. })
  136. }*/
  137. },
  138. /**
  139. * 更新底图信息面板
  140. * @param {[type]} layerObj [description]
  141. * @return {[type]} [description]
  142. */
  143. updatePanel: function(layerObj) {
  144. var _this = this;
  145. _this.curLayerObj = map23DData.layers[layerObj.guid]['layer'];
  146. _this.labelTypeP.innerHTML = _this.curLayerObj.name;
  147. _this.curLayerObj.translate = _this.curLayerObj.name;
  148. _this.curLayerObj.guid = _this.curLayerObj.guid;
  149. for (var i = 0; _this.selectZoom.options.length != 0; i++) {
  150. _this.selectZoom.options.remove(0);
  151. }
  152. for (var ii = _this.curLayerObj.minZoom; ii <= _this.curLayerObj.maxZoom; ii++) {
  153. var item = new Option(ii, ii);
  154. _this.selectZoom.options.add(item);
  155. }
  156. },
  157. /**
  158. * 获取当前底图信息并更新信息面板
  159. * @return {[type]} [description]
  160. */
  161. getBaseMap: function() {
  162. var _this = this;
  163. var baseMapObj = {
  164. translate: null,
  165. guid: null
  166. }
  167. baseMapObj.guid = ONEMAP.M.mapHolder.modValue.mainLayers;
  168. if (baseMapObj.guid === 'gm') {
  169. baseMapObj.translate = '交通图';
  170. baseMapObj.guid = 'gm';
  171. }
  172. if (baseMapObj.guid === 'gr') {
  173. baseMapObj.translate = '影像图';
  174. baseMapObj.guid = 'gr';
  175. }
  176. if (baseMapObj.guid === 'gr') {
  177. baseMapObj.translate = '影像叠加地名图';
  178. baseMapObj.guid = 'gr';
  179. }
  180. if (baseMapObj.guid === 'gt') {
  181. baseMapObj.translate = '地势图';
  182. baseMapObj.guid = 'gt';
  183. }
  184. _this.curLayerObj = baseMapObj;
  185. _this.labelTypeP.innerHTML = baseMapObj.translate;
  186. $(".zhengshiDl .name").attr("placeholder",baseMapObj.translate)
  187. for (var i = 0; _this.selectZoom.options.length != 0; i++) {
  188. _this.selectZoom.options.remove(0);
  189. }
  190. for (var ii = map23DConfig.map2DMinZoom; ii <= map23DConfig.map2DMaxZoom; ii++) {
  191. var item = new Option(ii, ii);
  192. _this.selectZoom.options.add(item);
  193. $(_this.selectZoom).val(_this._map.getZoom());
  194. }
  195. },
  196. updateShotBounds: function() {
  197. var _this = this;
  198. var bounds = _this.shotLayer.getBounds();
  199. _this.pLT.innerHTML = '[' + bounds.getNorthWest().lat.toFixed(5) + ',' + bounds.getNorthWest().lng.toFixed(5) + ']';
  200. _this.pRB.innerHTML = '[' + bounds.getSouthEast().lat.toFixed(5) + ',' + bounds.getSouthEast().lng.toFixed(5) + ']';
  201. $(_this.selectZoom).val(_this._map.getZoom());
  202. },
  203. /**
  204. * 标注创建 添加到容器
  205. * @param e
  206. */
  207. drawCreated: function(e) {
  208. var _this = this;
  209. var type = e.layerType;
  210. _this.shotLayer = e.layer;
  211. _this._shotGroup.addLayer(_this.shotLayer);
  212. _this.shotLayer.editing.enable();
  213. var thematic = {
  214. curThematic: {
  215. min_zoom: 1,
  216. max_zoom: 19,
  217. translate: "未知",
  218. guid: 'null'
  219. }
  220. };
  221. var baseMapObj = {
  222. translate: null,
  223. guid: null
  224. }
  225. baseMapObj.guid = ONEMAP.M.mapHolder.modValue.mainLayers;
  226. thematic.curThematic.min_zoom = map2DViewer.map.getMinZoom(); //baseMapObj.options.minZoom
  227. thematic.curThematic.max_zoom = map2DViewer.map.getMaxZoom();
  228. if (baseMapObj.guid === 'gm') {
  229. baseMapObj.translate = '交通图';
  230. baseMapObj.guid = 'gm';
  231. }
  232. if (baseMapObj.guid === 'gr') {
  233. baseMapObj.translate = '影像图';
  234. baseMapObj.guid = 'gr';
  235. }
  236. if (baseMapObj.guid === 'gr') {
  237. baseMapObj.translate = '影像叠加地名图';
  238. baseMapObj.guid = 'gr';
  239. }
  240. if (baseMapObj.guid === 'gt') {
  241. baseMapObj.translate = '地势图';
  242. baseMapObj.guid = 'gt';
  243. }
  244. thematic.curThematic.translate = baseMapObj.translate;
  245. thematic.curThematic.guid = baseMapObj.guid;
  246. var thematicData = ONEMAP.D.ThematicInMapData;
  247. // if (ONEMAP.D.overLayerCount > 0) {
  248. // $(_this.labelThematic).show();
  249. // $(_this.selectThematicType).show();
  250. // $(_this.labelTypeP).hide();
  251. // $(_this.labelTypeL).hide();
  252. // } else {
  253. // $(_this.labelThematic).hide();
  254. // $(_this.selectThematicType).hide();
  255. // $(_this.labelTypeP).show();
  256. // $(_this.labelTypeL).show();
  257. // }
  258. $(_this.selectThematicType).empty();
  259. $.each(thematicData, function(i, t) {
  260. var item = new Option(t.name, t.name);
  261. _this.selectThematicType.options.add(item);
  262. })
  263. _this.curLayerObj = thematic.curThematic;
  264. _this._map.on('editDraging', _this.updateShotBounds, this);
  265. var bounds = _this.shotLayer.getBounds();
  266. _this.pLT.innerHTML = '[' + bounds.getNorthWest().lat.toFixed(5) + ',' + bounds.getNorthWest().lng.toFixed(5) + ']';
  267. _this.pRB.innerHTML = '[' + bounds.getSouthEast().lat.toFixed(5) + ',' + bounds.getSouthEast().lng.toFixed(5) + ']';
  268. for (var i = 0; _this.selectZoom.options.length != 0; i++) {
  269. _this.selectZoom.options.remove(0);
  270. }
  271. for (var ii = thematic.curThematic.min_zoom; ii <= thematic.curThematic.max_zoom; ii++) {
  272. var item = new Option(ii, ii);
  273. _this.selectZoom.options.add(item);
  274. $(_this.selectZoom).val(_this._map.getZoom());
  275. }
  276. //_this.selectZoom.value = _this._map.getZoom();
  277. if(_this._msType == 1){
  278. _this.labelTypeP.innerHTML = thematic.curThematic.translate;
  279. _this.mapShotInfoContainer.style.display = 'block';
  280. } else{
  281. $(".zhengshiDl").addClass('active');
  282. $(".zhengshiDl .name").attr("placeholder",_this.curLayerObj.translate)
  283. $(".zhengshiDl .bili").val(_this._map.getZoom());
  284. $(".zhengshiDl .subdiv button").off("click").on("click",function(){
  285. _this._postScreenMapFishInfo();
  286. })
  287. }
  288. _this.layoutResize();
  289. },
  290. /**
  291. * 整视图下载
  292. */
  293. _postScreenMapFishInfo: function() {
  294. var _this = this;
  295. var thematicGuid;
  296. var bounds = _this.shotLayer.getBounds();
  297. _this.centerlng = (bounds.getNorthWest().lng+bounds.getSouthEast().lng).toFixed(5)/2;
  298. _this.centerlat = (bounds.getNorthWest().lat+bounds.getSouthEast().lat).toFixed(5)/2;
  299. newdata = new Date();
  300. year = newdata.getFullYear();
  301. month = newdata.getMonth() + 1;
  302. day = newdata.getDate().toString();
  303. var hour = newdata.getHours().toString();
  304. var mintes = newdata.getMinutes().toString();
  305. var second = newdata.getSeconds().toString();
  306. if (month < 10) {
  307. // month = "0" + month;
  308. }
  309. if (day < 10) {
  310. day = "0" + day;
  311. }
  312. if (hour < 10) {
  313. hour = "0" + hour;
  314. }
  315. if (mintes < 10) {
  316. mintes = "0" + mintes;
  317. }
  318. if (second < 10) {
  319. second = "0" + second;
  320. }
  321. var time = year + "年" + month + "月" + day + "日 " + hour + ":" + mintes + ":" + second ;
  322. if(_this.curLayerObj.guid == "gr"){
  323. _this.curLayerObj.guid = 'gh';
  324. }
  325. var svname = $(".zhengshiDl .name").val();
  326. if(svname == ""){
  327. svname = _this.curLayerObj.translate;
  328. }
  329. var svzoom = $('.zhengshiDl .bili option:selected').val();
  330. var svimgtype = $(".zhengshiDl .geshi option:selected").val();
  331. // $(_this.selectZoom).val()
  332. var postDataMapfish = {
  333. center: [_this.centerlng, _this.centerlat],
  334. name: svname,
  335. zoom: svzoom,//_this.selectZoom.value
  336. guid: _this.curLayerObj.guid,
  337. ext: svimgtype,//'png'
  338. mapshotName: svname,//_this.curLayerObj.translate
  339. mapshotcopyright: time
  340. }
  341. ONEMAP.C.publisher.publish(postDataMapfish, 'mapFish:postData');
  342. },
  343. /**
  344. * 提交数据到服务器
  345. * @type {Function}
  346. * @private
  347. */
  348. _postScreenShotInfo: function() {
  349. var _this = this;
  350. var thematicGuid;
  351. var bounds = _this.shotLayer.getBounds();
  352. // if (ONEMAP.D.overLayerCount > 0) {
  353. // var thematicName = $(_this.selectThematicType).val();
  354. // $.each(ONEMAP.D.ThematicInMapData, function(i, t) {
  355. // if (t.name === thematicName) {
  356. // thematicGuid = t.guid;
  357. // }
  358. // })
  359. // _this.curLayerObj.guid = thematicGuid;
  360. // _this.curLayerObj.translate = thematicName;
  361. // }
  362. var nwLat = bounds.getNorthWest().lat.toFixed(5);
  363. var nwLng = bounds.getNorthWest().lng.toFixed(5);
  364. var seLat = bounds.getSouthEast().lat.toFixed(5);
  365. var seLng = bounds.getSouthEast().lng.toFixed(5);
  366. if(nwLng<-180){
  367. ONEMAP.C.publisher.publish({ type: 'warning', message: '矩形左上经度不能小于-180度,请修改' }, 'noteBar::add');
  368. return false;
  369. }else if(seLng >180){
  370. ONEMAP.C.publisher.publish({ type: 'warning', message: '矩形右下经度大于180度,请修改' }, 'noteBar::add');
  371. return false;
  372. }
  373. if(_this.curLayerObj.guid == "gr"){
  374. _this.curLayerObj.guid = 'gh';
  375. }
  376. var postData = {
  377. bbox: '[' + bounds.getNorthWest().lng.toFixed(5) + ',' + bounds.getNorthWest().lat.toFixed(5) + ',' + bounds.getSouthEast().lng.toFixed(5) + ',' + bounds.getSouthEast().lat.toFixed(5) + ']',
  378. name: _this.curLayerObj.translate,
  379. zoom: _this.selectZoom.value,
  380. guid: _this.curLayerObj.guid,
  381. ext: _this.selectPicType.value
  382. }
  383. //_this._map.fire('mapShot:postData',postData);
  384. ONEMAP.C.publisher.publish(postData, 'mapShot:postData');
  385. //_this.remove();
  386. },
  387. /**
  388. * 移除事件
  389. * @type {Function}
  390. */
  391. remove: function() {
  392. var _this = this;
  393. if (_this._map != null) {
  394. _this._isScreenShot = false;
  395. L.DomUtil.removeClass(_this._container, 'active');
  396. _this.rectangle.disable();
  397. _this._shotGroup.clearLayers();
  398. _this.mapShotInfoContainer.style.display = 'none';
  399. _this._map.off('draw:created', _this.drawCreated, this);
  400. //_this._map.off('draw:edit', _this.drawEdit,this);
  401. _this._map.off('singleLayer:addToMap', _this.updatePanel, this);
  402. _this._map.off('singleLayer:removeFromMap', _this.getBaseMap, this);
  403. _this._map.off('editDraging', _this.updateShotBounds, this);
  404. _this._map.off('mainLayer:addLayer', _this.getBaseMap, this);
  405. _this._map.off('overLayer:addLayer', _this.getBaseMap, this);
  406. }
  407. }
  408. });
  409. L.control.mapShot = function(options) {
  410. return new L.Control.MapShot(options);
  411. };