mvtvectorlayer_mbstyle_linestyle.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <!-- <meta charset="UTF-8"> -->
  8. <title data-i18n="resources.title_mvtVectorLayer_mapboxStyle_lineStyle"></title>
  9. <script type="text/javascript" include='ol-mapbox-style' src="../../dist/ol/include-ol.js"></script>
  10. <style>
  11. ol {
  12. list-style: none;
  13. margin-left: -30px;
  14. }
  15. .ol-popup {
  16. position: absolute;
  17. background-color: rgba(0, 60, 136, 0.7);
  18. filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  19. padding: 10px;
  20. border-radius: 5px;
  21. border: 1px solid #cccccc;
  22. bottom: 20px;
  23. left: 10px;
  24. opacity: 0;
  25. transition: opacity 100ms ease-in;
  26. color: white;
  27. font-size: 15px;
  28. font-weight: bold;
  29. }
  30. #tipsPopup {
  31. background-color: rgba(0, 60, 136, 0.7);
  32. color: red;
  33. display: none;
  34. padding: 15px;
  35. color: white;
  36. font-size: 15px;
  37. font-weight: bold;
  38. border-radius: 5px;
  39. box-shadow: 0px -1px 46px 0px rgba(0, 0, 0, 0.75);
  40. }
  41. .editContainer {
  42. position: absolute;
  43. right: 15px;
  44. top: 50px;
  45. display: block;
  46. transition: opacity 100ms ease-in;
  47. border-radius: 5px;
  48. filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  49. box-shadow: 0px 0px 13px 0px rgba(25, 34, 41, 1);
  50. background: white;
  51. width: 300px;
  52. }
  53. .editContainer>.mainPanelHeader {
  54. width: 100%;
  55. padding: 8px 15px;
  56. background: rgb(0, 131, 203);
  57. font-size: 17px;
  58. font-weight: bold;
  59. color: white;
  60. }
  61. .editContainer>.mainPanelHeader>.closeButton {
  62. float: right;
  63. padding-left: 80px;
  64. font-size: 20px;
  65. cursor: pointer;
  66. }
  67. .editContainer>.editPanel {
  68. width: 100%;
  69. border: 1px solid grey;
  70. background: white;
  71. color: grey;
  72. }
  73. .layerType{
  74. margin-top: 20px;
  75. font-size: 16px;
  76. font-weight: bold;
  77. }
  78. .borderPanel,
  79. .fillPanel {
  80. background: white;
  81. margin: 10px;
  82. padding: 8px 12px 3px;
  83. box-shadow: inset 0px 0px 7px 0px rgba(43, 40, 43, 1);
  84. }
  85. .fillPanel {
  86. padding-right: 20px;
  87. }
  88. .borderPanelHeader,
  89. .fillPanelHeader {
  90. padding: 8px 0 8px;
  91. font-size: 15px;
  92. }
  93. .irs {
  94. height: 40px !important;
  95. }
  96. .irs-line,
  97. .irs-line-left,
  98. .irs-line-mid,
  99. .irs-line,
  100. .irs-bar,
  101. .irs-bar-edge {
  102. height: 5px !important;
  103. }
  104. .irs-min,
  105. .irs-max,
  106. .irs-from,
  107. .irs-to,
  108. .irs-single {
  109. font-size: 10px !important;
  110. }
  111. .irs-slider {
  112. top: 27px !important;
  113. width: 15px !important;
  114. height: 15px !important;
  115. }
  116. .alertContainer {
  117. width: 100%;
  118. text-align: center;
  119. position: absolute;
  120. top:10px;
  121. }
  122. .alertContainer > .alert{
  123. margin: auto;
  124. width: 300px;
  125. }
  126. </style>
  127. </head>
  128. <body style='margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;'>
  129. <div id='map' style='margin:0 auto;width: 100%;height: 100%'></div>
  130. <div id="popup" class="ol-popup">
  131. <div id="popup-content"></div>
  132. </div>
  133. <div id="tipsPopup">
  134. <span data-i18n="resources.text_tipsPopup_subtext"></span>
  135. </div>
  136. <div class="alertContainer">
  137. <div class="alert alert-info alert-dismissible" role="alert">
  138. <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  139. <span aria-hidden="true">&times;</span>
  140. </button>
  141. <strong data-i18n="resources.text_alert_subtext"></strong>
  142. </div>
  143. </div>
  144. <div class="editContainer" id="editorContainer">
  145. <div class="mainPanelHeader">
  146. <span data-i18n="resources.text_LineStyleEditor"></span>
  147. <span id="layer_name"></span>
  148. <span class="closeButton" id="closeButton">×</span>
  149. </div>
  150. <div class="editPanel">
  151. <div class="borderPanel">
  152. <div class="borderPanelHeader">
  153. <span data-i18n="resources.text_lineBorderLayer" class="layerType"></span>
  154. <span id="border_layers_id" style="display:none"></span>
  155. </div>
  156. <ol class="borderPanelList">
  157. <li>
  158. <label for="border_input_lineColor" data-i18n="resources.text_lineColor_1"></label>
  159. <input type="text" id="border_input_lineColor" value="" style="width:158px;" />
  160. <span>
  161. <img src='../img/colorpicker.png' id="border_input_lineColor_pic" style="cursor:pointer" />
  162. </span>
  163. </li>
  164. <li class="slider">
  165. <label for="border_input_lineOpacity" data-i18n="resources.text_lineOpacity"></label>
  166. <input type="text" id="border_input_lineOpacity" name="border_input_lineOpacity">
  167. </li>
  168. <li class="slider">
  169. <label for="border_input_lineWidth" data-i18n="resources.text_lineWidth_1"></label>
  170. <input type="text" id="border_input_lineWidth" name="border_input_lineWidth">
  171. </li>
  172. </ol>
  173. </div>
  174. <div class="fillPanel">
  175. <div class="fillPanelHeader">
  176. <span data-i18n="resources.text_lineFillLayer" class="layerType"></span>
  177. <span id="fill_layers_id" style = "display:none"></span>
  178. </div>
  179. <ol class="fillPanelList">
  180. <li>
  181. <label for="fill_input_lineColor" data-i18n="resources.text_lineColor_1"></label>
  182. <input type="text" id="fill_input_lineColor" value="" style="width:158px;" />
  183. <span>
  184. <img src='../img/colorpicker.png' id="fill_input_lineColor_pic" style="cursor:pointer" />
  185. </span>
  186. </li>
  187. <li class="slider">
  188. <label for="fill_input_lineOpacity" name="fill_input_lineOpacity" data-i18n="resources.text_lineOpacity"></label>
  189. <input type="text" id="fill_input_lineOpacity">
  190. </li>
  191. <li class="slider">
  192. <label for="fill_input_lineWidth" data-i18n="resources.text_lineWidth_1"></label>
  193. <input type="text" id="fill_input_lineWidth" name="fill_input_lineWidth">
  194. </li>
  195. </ol>
  196. </div>
  197. </div>
  198. </div>
  199. <script type="text/javascript" include="bootstrap,jquery,colorpicker,ionRangeSlider" src="../js/include-web.js"></script>
  200. <script type="text/javascript">
  201. var url = (window.isLocal ? window.server : "https://iserver.supermap.io") +
  202. "/iserver/services/map-mvt-California/rest/maps/California";
  203. getResolutions = function (zoom, scale, targetMinZoom, targetMaxZoom) {
  204. var res = scaleToResolution(scale);
  205. var minRes = res * Math.pow(2, zoom - targetMinZoom);
  206. var resolutions = [];
  207. for (var index = 0; index < targetMaxZoom - targetMinZoom + 1; index++) {
  208. resolutions.push(minRes / Math.pow(2, index));
  209. }
  210. return resolutions;
  211. }
  212. scaleToResolution = function (scale) {
  213. var inchPerMeter = 1 / 0.0254;
  214. var meterPerMapUnitValue = Math.PI * 2 * 6378137 / 360;
  215. return 1 / (scale * 96 * inchPerMeter * meterPerMapUnitValue);
  216. };
  217. var vectorLayer;
  218. var resolutions = getResolutions(10, 0.00000346145499464224, 0, 16);
  219. var map = new ol.Map({
  220. target: 'map',
  221. view: new ol.View({
  222. center: [-122.228687503369, 38.1364932162598],
  223. zoom: 13,
  224. minZoom: 13,
  225. maxZoom: 13,
  226. projection: 'EPSG:4326',
  227. resolutions: resolutions
  228. })
  229. });
  230. var container = document.getElementById('popup');
  231. var content = document.getElementById('popup-content');
  232. var editorContainer = document.getElementById('editorContainer');
  233. var tipsPopupContainer = document.getElementById('tipsPopup');
  234. info = new ol.control.Control({
  235. element: container
  236. });
  237. var editorContainerInfo = new ol.control.Control({
  238. element: editorContainer
  239. });
  240. info.setMap(map);
  241. editorContainerInfo.setMap(map);
  242. map.addControl(info);
  243. map.addControl(editorContainerInfo);
  244. var tipsPopup = new ol.Overlay({
  245. element: tipsPopupContainer,
  246. offset: [15, -50]
  247. });
  248. map.addOverlay(tipsPopup);
  249. var format = new ol.format.MVT({
  250. featureClass: ol.Feature
  251. });
  252. var style = new ol.supermap.MapboxStyles({
  253. url: url,
  254. map: map,
  255. resolutions: resolutions
  256. })
  257. var source = new ol.source.VectorTileSuperMapRest({
  258. url: url,
  259. projection: 'EPSG:4326',
  260. tileGrid: new ol.tilegrid.TileGrid({
  261. resolutions: resolutions,
  262. origin: [-180, 90],
  263. tileSize: 512
  264. }),
  265. tileType: 'ScaleXY',
  266. format: format
  267. })
  268. style.on('styleloaded', function () {
  269. vectorLayer = new ol.layer.VectorTile({
  270. //设置避让参数
  271. declutter: true,
  272. source: source,
  273. style: style.getStyleFunction()
  274. });
  275. map.addLayer(vectorLayer);
  276. setDefaultEditor("Motorway_L@California");
  277. })
  278. map.on('pointermove', function (e) {
  279. var features = map.getFeaturesAtPixel(e.pixel);
  280. if (!features || features.length === 0) {
  281. content.innerHTML = '';
  282. container.style.opacity = 0;
  283. tipsPopupContainer.style.display = "none";
  284. return;
  285. }
  286. content.innerHTML = "Layer: " + features[0].get('layer') + "<br />" + (features[0].get('NAME') ?
  287. "Name: " + features[0].get('NAME') : "");
  288. container.style.opacity = 1;
  289. var layerName = features[0].get('layer');
  290. var layerStyleArr = style.getStylesBySourceLayer(layerName);
  291. if (layerStyleArr[0].type !== 'line') {
  292. tipsPopupContainer.style.display = "none";
  293. return;
  294. }
  295. tipsPopupContainer.style.display = "block";
  296. tipsPopup.setPosition(e.coordinate);
  297. });
  298. // 获取线边框jq对象
  299. var borderJqObject = [];
  300. borderJqObject['id'] = $("#border_layers_id");
  301. borderJqObject['lineColor'] = $("#border_input_lineColor");
  302. borderJqObject['lineOpacity'] = $("#border_input_lineOpacity");
  303. borderJqObject['lineWidth'] = $("#border_input_lineWidth");
  304. borderJqObject['pic'] = $("#border_input_lineColor_pic");
  305. // 获取线填充jq对象
  306. var fillJqObject = [];
  307. fillJqObject['id'] = $("#fill_layers_id")
  308. fillJqObject['lineColor'] = $("#fill_input_lineColor");
  309. fillJqObject['lineOpacity'] = $("#fill_input_lineOpacity");
  310. fillJqObject['lineWidth'] = $("#fill_input_lineWidth");
  311. fillJqObject['pic'] = $("#fill_input_lineColor_pic");
  312. // 点击弹出编辑框
  313. map.on('click', function (e) {
  314. var features = map.getFeaturesAtPixel(e.pixel);
  315. if (!features || features.length === 0) {
  316. resetStyle(borderJqObject);
  317. resetStyle(fillJqObject);
  318. return;
  319. }
  320. var layerName = features[0].get('layer');
  321. setDefaultEditor(layerName);
  322. });
  323. // 展示图层信息
  324. function showLayerInfo(layerInfo, jqObject) {
  325. var layerStyle = layerInfo.style;
  326. $('#layer_name').html(layerInfo.layerName);
  327. jqObject['id'].html(layerInfo.id);
  328. jqObject['lineColor'].val(layerStyle.lineColor);
  329. jqObject['lineOpacity'].val(layerStyle.lineOpacity);
  330. jqObject['lineWidth'].val(layerStyle.lineWidth);
  331. // 取色器
  332. colorPicker(jqObject);
  333. var lineOpacityOption = { min: "0", max: "1", step: "0.1", from: layerStyle.lineOpacity, opacitySlider: "true" };
  334. var lineWidthOption = { min: "0", max: "50", step: "0.5", from: layerStyle.lineWidth, opacitySlider: "false" }
  335. // 透明度滑块
  336. slider(jqObject['lineOpacity'], jqObject, lineOpacityOption);
  337. // 宽度滑块
  338. slider(jqObject['lineWidth'], jqObject, lineWidthOption);
  339. }
  340. // 设置取色器模块
  341. function colorPicker(jqObject) {
  342. jqObject['pic'].colorpicker({
  343. ishex: false,
  344. fillcolor: true,
  345. event: 'mouseover',
  346. target: jqObject['lineColor'],
  347. success: function () {
  348. var lineColor = jqObject['lineColor'].val().toString().match(/\d+/g);
  349. var opacity = jqObject['lineOpacity'].val();
  350. lineColor.push(opacity);
  351. lineColor = "rgba(" + lineColor.join(",") + ")"
  352. var styleOption = getEditOption(jqObject);
  353. styleOption.style.lineColor = lineColor;
  354. changeStyle(styleOption);
  355. }
  356. });
  357. }
  358. // 设置滑动模块
  359. function slider(target, jqObject, option) {
  360. target.ionRangeSlider({
  361. type: "single",
  362. min: option.min,
  363. max: option.max,
  364. step: option.step,
  365. from: option.from,
  366. onChange: function (data) {
  367. var data = data.from;
  368. var styleOption = getEditOption(jqObject);
  369. if (option.opacitySlider === "true") {
  370. styleOption.style.lineOpacity = data;
  371. } else {
  372. styleOption.style.lineWidth = data;
  373. }
  374. changeStyle(styleOption);
  375. }
  376. });
  377. var sliderElement = target.data("ionRangeSlider");
  378. sliderElement.update({
  379. from: option.from
  380. });
  381. }
  382. // 改变样式
  383. function changeStyle(styleOption) {
  384. style.updateStyles([{
  385. "id": styleOption.id,
  386. "layout":{
  387. "line-cap":'round'
  388. },
  389. "paint": {
  390. "line-width": styleOption.style.lineWidth,
  391. "line-color": styleOption.style.lineColor
  392. }
  393. }]);
  394. source.changed();
  395. }
  396. // 获得编辑信息
  397. function getEditOption(jqObject) {
  398. var layerId = jqObject['id'].html();
  399. var lineColor = jqObject['lineColor'].val();
  400. var lineOpacity = jqObject['lineOpacity'].val();
  401. var lineWidth = jqObject['lineWidth'].val();
  402. lineColor = lineColor.toString().match(/\d+/g);
  403. lineColor.push(lineOpacity);
  404. lineColor = "rgba(" + lineColor.join(",") + ")";
  405. return {
  406. id: layerId,
  407. style: {
  408. lineColor: lineColor,
  409. lineWidth: lineWidth
  410. }
  411. }
  412. }
  413. // 获取图层信息
  414. function getLayerInfo(layerName, layerStyle, layerType) {
  415. if (layerType !== "line") {
  416. resetStyle(borderJqObject);
  417. resetStyle(fillJqObject);
  418. return;
  419. }
  420. var styleObj = getStyleObject(layerStyle);
  421. return {
  422. layerName: layerName,
  423. id: layerStyle.id,
  424. style: styleObj
  425. }
  426. }
  427. // 获取图层样式
  428. function getStyleObject(layerStyle) {
  429. var type = layerStyle.type;
  430. var paint = layerStyle.paint;
  431. var lineColor = paint["line-color"].toString().match(/\d+/g);
  432. if (lineColor[4]) {
  433. var opacity = lineColor[3].toString() + "." + lineColor[4].toString();
  434. }
  435. var opacity = lineColor[3].toString();
  436. lineColor = lineColor.slice(0, 3);
  437. lineColor = "rgb(" + lineColor.join(",") + ")";
  438. return {
  439. "lineColor": lineColor,
  440. "lineOpacity": opacity,
  441. "lineWidth": paint["line-width"]
  442. }
  443. }
  444. function resetStyle(jqObject) {
  445. jqObject['id'].html("");
  446. jqObject['lineColor'].val("");
  447. jqObject['lineOpacity'].val("");
  448. jqObject['lineWidth'].val("")
  449. editorContainer.style.display = "none";
  450. }
  451. // 设置默认编辑器
  452. function setDefaultEditor(layerName) {
  453. var layerStyleArr = style.getStylesBySourceLayer(layerName);
  454. if (layerStyleArr[0].type !== 'line') {
  455. resetStyle(borderJqObject);
  456. resetStyle(fillJqObject);
  457. return;
  458. }
  459. var layerStyle = [];
  460. for (var i = 0; i < layerStyleArr.length; i++) {
  461. if (layerStyleArr[i].maxzoom === 14) {
  462. layerStyle.push(layerStyleArr[i]);
  463. }
  464. }
  465. var layerType = layerStyle[0].type;
  466. // 线边框图层样式
  467. var lineBorderStyle = layerStyle[0];
  468. // 线填充图层样式
  469. var lineFillStyle = layerStyle[1];
  470. var lineBorderInfo = getLayerInfo(layerName, lineBorderStyle, layerType);
  471. var lineFillInfo = getLayerInfo(layerName, lineFillStyle, layerType);
  472. showLayerInfo(lineBorderInfo, borderJqObject, "boder");
  473. showLayerInfo(lineFillInfo, fillJqObject, "fill");
  474. editorContainer.style.display = "block";
  475. }
  476. // 点击关闭编辑器
  477. $('#closeButton').click(function () {
  478. editorContainer.style.display = "none";
  479. });
  480. </script>
  481. </body>
  482. </html>