addMap.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  7. <title>可视化编辑</title>
  8. <link rel="stylesheet" href="../../../../assets/libs/layui/css/layui.css"/>
  9. <link rel="stylesheet" href="../../../../assets/module/admin.css?v=312"/>
  10. <link rel="stylesheet" href="../../../../assets/css/editormap.css?v=312"/>
  11. <link rel="stylesheet" type="text/css" href="../../../../assets/libs/leaflet/leaflet-draw/leaflet.draw-src.css">
  12. <link rel="stylesheet" type="text/css" href="../../../../assets/libs/leaflet/leaflet.css">
  13. <link rel="stylesheet" href="../../../../assets/module/formSelects/formSelects-v4.css"/>
  14. </head>
  15. <body>
  16. <!-- 页面加载loading -->
  17. <div class="page-loading">
  18. <div class="ball-loader">
  19. <span></span><span></span><span></span><span></span>
  20. </div>
  21. </div>
  22. <!-- 正文开始 -->
  23. <div class="layui-fluid">
  24. <div class="layui-card">
  25. <div class="layui-card-body">
  26. <form id="modelRoleForm" lay-filter="modelRoleForm" class="layui-form">
  27. <input type="hidden" name="entMapId" id="entMapId">
  28. <div class="layui-form-item">
  29. <div class="layui-inline">
  30. <label class="layui-form-label w-auto w-padding">选择背景图:</label>
  31. <div class="layui-input-inline mr0">
  32. <select id="entMapList" name="entMapList" lay-filter="entMapList" lay-verType="tips"
  33. class="layui-input-block">
  34. <option value="">选择背景图</option>
  35. </select>
  36. </div>
  37. </div>
  38. </div>
  39. <div class="map-container" id="map" style="height: 500px"></div>
  40. <div class="layui-form-item text-right">
  41. <button class="layui-btn" lay-filter="modelSubmitRole" lay-submit>保存</button>
  42. <button class="layui-btn layui-btn-primary" type="button" id="closePageDialog">取消</button>
  43. </div>
  44. </form>
  45. </div>
  46. </div>
  47. </div>
  48. <!-- js部分 -->
  49. <script type="text/javascript" src="../../../../assets/libs/layui/layui.js"></script>
  50. <script type="text/javascript" src="../../../../assets/js/common.js?v=312"></script>
  51. <script type="text/javascript" src="../../../../assets/libs/jquery/jquery-3.2.1.min.js"></script>
  52. <script src="../../../../assets/libs/leaflet/leaflet.js"></script>
  53. <script src="../../../../assets/libs/leaflet/leaflet-draw/leaflet.draw-src.js"></script>
  54. <script src="../../../../assets/libs/leaflet/leaflet-draw/local.js"></script>
  55. <script src="../../../../assets/libs/leaflet/proj4-compressed.js"></script>
  56. <script src="../../../../assets/libs/leaflet/proj4leaflet.js"></script>
  57. <script>
  58. var imgPath;
  59. var map;
  60. var entMapList;
  61. var loadFlag = true;
  62. layui.use(['layer', 'form', 'uParas', 'admin'], function () {
  63. var $ = layui.jquery;
  64. var layer = layui.layer;
  65. var form = layui.form;
  66. var admin = layui.admin;
  67. var uParas = layui.uParas;
  68. var riskPointId = uParas.getUrlParam("riskPointId");
  69. var entMapId = uParas.getUrlParam("entMapId");
  70. if (entMapId != null && entMapId != undefined) {
  71. $("#entMapId").val(entMapId);
  72. }
  73. $.ajaxSetup({async: false});
  74. $.getJSON(uParas.baseUrl + "/ent/map/getList", {mapType: 2}, function (result) {
  75. var entMap;
  76. if (result.data != null) {
  77. entMapList = result.data;
  78. if (entMapId != null && entMapId != undefined) {
  79. $.each(result.data, function (i, field) {
  80. if (field.mapId.toString() === entMapId) {
  81. imgPath = field.mapRasterLayer;
  82. uParas.initSimpleSelectX("entMapList", "modelRoleForm", result, "mapName", "mapId", entMapId);
  83. return;
  84. }
  85. });
  86. } else {
  87. entMap = result.data[0];
  88. imgPath = entMap.mapRasterLayer;
  89. uParas.initSimpleSelectX("entMapList", "modelRoleForm", result, "mapName", "mapId", entMap.mapId);
  90. $("#entMapId").val(entMap.mapId);
  91. }
  92. }
  93. });
  94. // console.log("entMapList", entMapList);
  95. //监听下拉选择
  96. form.on('select(entMapList)', function (data) {
  97. $.each(entMapList, function (i, field) {
  98. if (field.mapId.toString() === data.value) {
  99. imgPath = field.mapRasterLayer;
  100. console.log(field)
  101. return;
  102. }
  103. });
  104. $("#entMapId").val(data.value);
  105. myBmap.resetMapLoadData(riskPointId);
  106. });
  107. form.on('submit(modelSubmitRole)', function (data) {
  108. //console.log(data.field);
  109. var layers = plotLayer.getLayers();
  110. var drawIds = '';
  111. if (layers.length > 0) {
  112. layers.forEach(function (v) {
  113. v = v.toGeoJSON();
  114. //console.log(v);
  115. if (v.geometry.type == "Point") {
  116. var data = new Array();
  117. data[0] = v.geometry.coordinates[1];
  118. data[1] = v.geometry.coordinates[0];
  119. drawIds = drawIds += JSON.stringify(data) + ";" + v.geometry.type + "#";
  120. } else {
  121. drawIds = drawIds += JSON.stringify(v.geometry.coordinates) + ";" + v.geometry.type + "#";
  122. }
  123. });
  124. }
  125. data.field.riskPointId = riskPointId;
  126. data.field.drawIds = drawIds;
  127. data.field.entMapId = $("#entMapId").val();
  128. console.log(data.field.entMapId)
  129. $.ajax({
  130. type: "post",
  131. url: "/ent/riskPointMap/saveDangerSourceMapData",
  132. data: data.field,
  133. dataType: "json",
  134. success: function (result) {
  135. if (result.code === 1) {
  136. parent.layer.closeAll();
  137. layer.msg(result.msg);
  138. parent.closeReload();
  139. } else {
  140. layer.msg(result.msg);
  141. }
  142. }
  143. });
  144. return false;
  145. });
  146. $('#closePageDialog').click(function () {
  147. parent.layer.closeAll();
  148. });
  149. myBmap.init();
  150. myBmap.resetMapLoadData(riskPointId);
  151. });
  152. var myBmap = {
  153. mapContainer: "map", //地图容器
  154. mapTile: "http://online{s}.map.bdimg.com/tile/?qt=vtile&x={x}&y={y}&z={z}&styles=pl&scaler=2&udt=20181205", //切片图层
  155. myIcon: null, //保存创建的icon
  156. myMarker: null, //保存自己创建marker
  157. myMarkerType: 0, //marker类型
  158. myMarkerId: null, //markerId
  159. isSave: true, //是否保存标记
  160. checkedIcon: null, //保存选中的icon
  161. deleteMarkerId: null, //保存删除得markerId
  162. mapControl: null, // 地图控制器,用来清除叠加控制
  163. data: {},
  164. layerGroup: null, // 全部图层组
  165. plotLayer: null, // 绘制图层组
  166. init: function () {
  167. var _this = this;
  168. // 初始化图层组
  169. layerGroup = new L.LayerGroup()
  170. //初始化地图
  171. var map_type = 3;
  172. if (map_type == 4) {
  173. baidu_map();
  174. } else {
  175. image_map();
  176. }
  177. // 增加区域绘制工具栏
  178. plotLayer = new L.FeatureGroup(),
  179. map.addLayer(plotLayer);
  180. var e = {
  181. position: "topright",
  182. draw: {
  183. polyline: {shapeOptions: {color: "#0000ff", weight: 3}},
  184. polygon: {allowIntersection: !0, showArea: !0, shapeOptions: {color: "#0000ff", weight: 2}},
  185. rectangle: {shapeOptions: {color: "#0000ff", weight: 2}},
  186. circle: false,
  187. circlemarker: false,
  188. marker: true,
  189. Point: false
  190. },
  191. edit: {
  192. featureGroup: plotLayer,
  193. remove: !0,
  194. edit: true
  195. }
  196. }, n = mapControl = new L.Control.Draw(e);
  197. // if (loadFlag) {
  198. // map.addControl(n),
  199. // map.on(L.Draw.Event.CREATED, draw_createdHandler);
  200. // }
  201. map.addControl(n),
  202. map.off(L.Draw.Event.CREATED).on(L.Draw.Event.CREATED, draw_createdHandler);
  203. function draw_createdHandler(t) {
  204. // console.dir(t)
  205. t.layerType;
  206. var o = t.layer;
  207. // 逻辑判断
  208. // var layers = plotLayer.getLayers();
  209. // var types;
  210. // if (layers.length > 0) {
  211. // layers.forEach(function (v) {
  212. // types += v.toGeoJSON().geometry.type;
  213. // });
  214. // if (types.indexOf("Point") != -1) {
  215. // Point = 1
  216. // }
  217. // }
  218. // if (o.toGeoJSON().geometry.type == "Point" && Point == 0) {
  219. // plotLayer.addLayer(o);
  220. // } else if (o.toGeoJSON().geometry.type != "Point") {
  221. // plotLayer.addLayer(o);
  222. // }
  223. plotLayer.addLayer(o);
  224. myBmap.addLayer(o)
  225. }
  226. //底图为图片
  227. function image_map() {
  228. if (map == undefined || map._loaded == undefined) {
  229. map = L.map(_this.mapContainer, {
  230. crs: L.CRS.Simple,
  231. center: L.latLng(50.5, 30.5),
  232. minZoom: 0,
  233. zoom: 4,
  234. attributionControl: false
  235. });
  236. } else {
  237. loadFlag = false;
  238. }
  239. }
  240. //底图为百度地图
  241. function baidu_map() {
  242. var maxZoom = 26;
  243. var crs = new L.Proj.CRS('EPSG:3395',
  244. '+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs', {
  245. resolutions: function () {
  246. level = 19;
  247. var res = [];
  248. res[0] = Math.pow(2, 18);
  249. for (var i = 1; i < level; i++) {
  250. res[i] = Math.pow(2, (18 - i))
  251. }
  252. return res;
  253. }(),
  254. origin: [0, 0],
  255. bounds: L.bounds([20037508.342789244, 0], [0, 20037508.342789244])
  256. });
  257. var image = new L.TileLayer(_this.mapTile, {
  258. maxZoom: maxZoom,
  259. minZoom: 10,
  260. subdomains: [0, 1, 2],
  261. tms: true
  262. });
  263. map = L.map(_this.mapContainer, {
  264. crs: crs,
  265. layers: [image]
  266. });
  267. map.setView([34.474387, 109.490437], 14);
  268. }
  269. },
  270. resetMapLoadData(riskPointId) { // 重置地图加载数据
  271. myBmap.clearLayer()
  272. myBmap.loadData(riskPointId)
  273. myBmap.resetMapImage()
  274. },
  275. /**
  276. * @Author: NanKeYang
  277. * @Description: 重置地图图片
  278. * @Data: 2019/12/23 10:59
  279. */
  280. resetMapImage: function () {
  281. var yx = L.latLng;
  282. var xy = function xy(x, y) {
  283. if (L.Util.isArray(x)) {
  284. return yx(x[1], x[0]);
  285. }
  286. return yx(y, x);
  287. };
  288. // map.eachLayer(function (layer) {
  289. // console.log(layer)
  290. // // map.removeLayer(layer);
  291. // });
  292. var bounds = [xy(-600, -600), xy(300, 300)];
  293. let imgOverlay = L.imageOverlay(imgPath, bounds).addTo(map);
  294. myBmap.addLayer(imgOverlay);
  295. map.fitBounds(bounds);
  296. },
  297. /**
  298. * @Author: NanKeYang
  299. * @Description: 加载数据
  300. * @Data: 2019/12/23 10:59
  301. */
  302. loadData: function (riskPointId) {
  303. //获取数据
  304. $.getJSON("/ent/riskPointMap/findMapDataById", {
  305. riskPointId: riskPointId,
  306. entMapId: $("#entMapId").val()
  307. }, function (result) {
  308. $.each(result.data, function (index, item) {
  309. //console.log(item);
  310. var markerData = item.markerData;
  311. if (markerData != null) {
  312. var optionsData = {
  313. riskPointId: item.riskPointId,
  314. title: item.markerTitle
  315. };
  316. var layers;
  317. if (item.markerType == 3) {
  318. var data = new Array();
  319. $.each($.parseJSON(markerData), function (index, item) {
  320. data[index] = new Array(item[1], item[0]);
  321. });
  322. layers = L.polygon(data, optionsData);
  323. } else if (item.markerType == 1) {
  324. layers = L.marker($.parseJSON(markerData));
  325. } else if (item.markerType == 2) {
  326. var data = new Array();
  327. $.each($.parseJSON(markerData), function (index, item) {
  328. data[index] = new Array(item[1], item[0]);
  329. });
  330. layers = L.polyline(data, optionsData);
  331. }
  332. plotLayer.addLayer(layers);
  333. myBmap.addLayer(layers)
  334. }
  335. });
  336. });
  337. },
  338. /**
  339. * @Author: NanKeYang
  340. * @Description: 添加图层
  341. * @Data: 2019/12/23 11:00
  342. */
  343. addLayer: function (layer) {
  344. layerGroup.addLayer(layer)
  345. },
  346. /**
  347. * @Author: NanKeYang
  348. * @Description: 清空图层
  349. * @Data: 2019/12/23 11:00
  350. */
  351. clearLayer: function () {
  352. layerGroup.eachLayer(function (layerItem) {
  353. map.removeLayer(layerItem)
  354. })
  355. layerGroup.clearLayers()
  356. plotLayer.clearLayers()
  357. },
  358. /**
  359. * @Author: NanKeYang
  360. * @Description: 清除地图控制
  361. * @Data: 2019/12/23 11:00
  362. */
  363. clearMapController() { // 清除地图控制器
  364. map.removeControl(mapControl)
  365. }
  366. }
  367. function iconClick(e) {
  368. $(e).toggleClass('icon-active')
  369. }
  370. </script>
  371. </body>
  372. </html>