mvtvectorlayer_mbstyle_beijing.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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"></title>
  9. <style>
  10. #menu {
  11. position: absolute;
  12. z-index: 2;
  13. padding: 6px 0 6px 10px;
  14. margin-right: 20px;
  15. left: 60px;
  16. top: 10px;
  17. float: left;
  18. border-radius: 4px;
  19. -moz-box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.1);
  20. -webkit-box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.1);
  21. box-shadow: 0px 0px 0px 2px rgba(0, 0, 0, 0.1);
  22. overflow: hidden;
  23. background: #fff;
  24. }
  25. label {
  26. margin: 0;
  27. margin-right: 10px;
  28. }
  29. .ol-popup {
  30. position: absolute;
  31. background-color: white;
  32. -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  33. filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  34. padding: 15px;
  35. border-radius: 10px;
  36. border: 1px solid #cccccc;
  37. bottom: 12px;
  38. left: -50px;
  39. min-width: 280px;
  40. }
  41. .ol-popup:after,
  42. .ol-popup:before {
  43. top: 100%;
  44. border: solid transparent;
  45. content: " ";
  46. height: 0;
  47. width: 0;
  48. position: absolute;
  49. pointer-events: none;
  50. }
  51. .ol-popup:after {
  52. border-top-color: white;
  53. border-width: 10px;
  54. left: 48px;
  55. margin-left: -10px;
  56. }
  57. .ol-popup:before {
  58. border-top-color: #cccccc;
  59. border-width: 11px;
  60. left: 48px;
  61. margin-left: -11px;
  62. }
  63. .ol-popup-closer {
  64. text-decoration: none;
  65. position: absolute;
  66. top: 2px;
  67. right: 8px;
  68. }
  69. .ol-popup-closer:after {
  70. content: "✖";
  71. }
  72. </style>
  73. </head>
  74. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
  75. <div id="map" style="margin:0 auto;width: 100%;height: 100%;"></div>
  76. <div id="popup" class="ol-popup">
  77. <a href="#" id="popup-closer" class="ol-popup-closer"></a>
  78. <div id="popup-content"></div>
  79. </div>
  80. <div id='menu' class="mapboxgl-ctrl">
  81. <input id='basic' type='radio' name='rtoggle' value='basic' checked='checked'>
  82. <label for='basic' data-i18n="resources.text_vectorDefaultStyle"></label>
  83. <input id='dark' type='radio' name='rtoggle' value='dark'>
  84. <label for='dark' data-i18n="resources.text_darkBlueStyle"></label>
  85. <input id='fiordcolor' type='radio' name='rtoggle' value='fiordcolor'>
  86. <label for='fiordcolor' data-i18n="resources.text_fiordStyle"></label>
  87. <input id='klokantech' type='radio' name='rtoggle' value='klokantech'>
  88. <label for='klokantech' data-i18n="resources.text_klokantechStyle"></label>
  89. <input id='osm' type='radio' name='rtoggle' value='OSM'>
  90. <label for='osm' data-i18n="resources.text_osmStyle"></label>
  91. <input id='positron' type='radio' name='rtoggle' value='positron'>
  92. <label for='positron' data-i18n="resources.text_positronStyle"></label>
  93. </div>
  94. <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  95. <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/i18next/10.0.7/i18next.min.js"></script>
  96. <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery-i18next/1.2.1/jquery-i18next.min.js"></script>
  97. <script type="text/javascript" src="../js/utils.js"></script>
  98. <script type="text/javascript" src=".././locales/zh-CN/resources.js"></script>
  99. <script type="text/javascript" src="../js/localization.js"></script>
  100. <script>
  101. Localization.initializeI18N('../', function () {
  102. Localization.localize();
  103. Localization.initGlobal();
  104. });
  105. </script>
  106. <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
  107. <script type="text/javascript" include='ol-mapbox-style' src="../../dist/ol/include-ol.js"></script>
  108. <script type="text/javascript">
  109. var url = (window.isLocal ? window.server : "https://iserver.supermap.io") +
  110. "/iserver/services/map-beijing/rest/maps/beijingMap";
  111. /**
  112. * Elements that make up the popup.
  113. */
  114. var container = document.getElementById('popup');
  115. var content = document.getElementById('popup-content');
  116. var closer = document.getElementById('popup-closer');
  117. /**
  118. * Create an overlay to anchor the popup to the map.
  119. */
  120. var overlay = new ol.Overlay({
  121. element: container,
  122. autoPan: true,
  123. autoPanAnimation: {
  124. duration: 250
  125. }
  126. });
  127. /**
  128. * Add a click handler to hide the popup.
  129. * @return {boolean} Don't follow the href.
  130. */
  131. closer.onclick = function () {
  132. overlay.setPosition(undefined);
  133. closer.blur();
  134. return false;
  135. };
  136. var stylesJson = {
  137. 'basic': "beijing.json",
  138. 'dark': "beijingDark.json",
  139. 'fiordcolor': "beijingFiord.json",
  140. 'klokantech': "beijingKlokantech.json",
  141. 'osm': "beijingOSM.json",
  142. 'positron': "beijingPositron.json"
  143. }
  144. var vectorLayer, currentStyleID, styles = {};
  145. var selectID = 'basic';
  146. var map = new ol.Map({
  147. target: 'map',
  148. overlays: [overlay],
  149. controls: ol.control.defaults({
  150. attributionOptions: {
  151. collapsed: false
  152. }
  153. })
  154. .extend([new ol.supermap.control.Logo()]),
  155. view: new ol.View({
  156. maxResolution: 40075016.68557849 / 512,
  157. center: ol.proj.fromLonLat([116.4, 39.9]),
  158. zoom: 11
  159. })
  160. });
  161. var targetStyle;
  162. switchStyle(null, 'basic');
  163. map.on('click', function (e) {
  164. var features = map.getFeaturesAtPixel(e.pixel);
  165. if (features && features.length > 0) {
  166. var coordinate = e.coordinate;
  167. content.innerHTML = "Layer: " + features[0].get('layer') + "<br />" +
  168. (features[0].get('NAME') ? "Name: " + features[0].get('NAME') + "<br />" : "") +
  169. (features[0].get('道路名称') ? "道路名称: " + features[0].get('道路名称') + "<br />" : "");
  170. overlay.setPosition(coordinate);
  171. targetStyle.setSelectedId(features[0].getId(), features[0].get('layer'));
  172. vectorLayer.changed();
  173. }
  174. });
  175. var layerList = document.getElementById('menu');
  176. var inputs = layerList.getElementsByTagName('input');
  177. for (var i = 0; i < inputs.length; i++) {
  178. inputs[i].onclick = switchStyle;
  179. }
  180. function switchStyle(e, targetStyleID) {
  181. selectID = targetStyleID || e.target.id;
  182. if (selectID === currentStyleID) {
  183. return;
  184. }
  185. overlay.setPosition(undefined);
  186. closer.blur();
  187. if (styles[selectID]) {
  188. targetStyle.setStyle(styles[selectID]);
  189. } else {
  190. $.get('../data/styles/' + stylesJson[selectID], function (json) {
  191. styles[selectID] = json;
  192. if (targetStyle) {
  193. targetStyle.setStyle(json);
  194. } else {
  195. targetStyle = new ol.supermap.MapboxStyles({
  196. source: 'vector-tiles',
  197. map: map,
  198. style: json
  199. });
  200. targetStyle.on('styleloaded', function () {
  201. styleLoaded(selectID);
  202. });
  203. }
  204. })
  205. }
  206. }
  207. function styleLoaded(styleID) {
  208. currentStyleID = styleID;
  209. if (!vectorLayer) {
  210. var vectorTileOptions = {
  211. url: url,
  212. format: new ol.format.MVT({
  213. featureClass: ol.Feature
  214. }),
  215. tileType: 'ViewBounds'
  216. };
  217. vectorLayer = new ol.layer.VectorTile({
  218. //设置避让参数
  219. declutter: true,
  220. style: targetStyle.getStyleFunction(),
  221. source: new ol.source.VectorTileSuperMapRest(vectorTileOptions),
  222. });
  223. map.addLayer(vectorLayer);
  224. } else {
  225. vectorLayer.changed();
  226. }
  227. }
  228. </script>
  229. </body>
  230. </html>