mvtvectorlayer_mbstyle_4326.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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"></title>
  9. <script type="text/javascript" src="../js/include-web.js"></script>
  10. <script type="text/javascript" include='ol-mapbox-style' src="../../dist/ol/include-ol.js"></script>
  11. <style>
  12. .ol-popup {
  13. position: absolute;
  14. background-color: rgba(0, 60, 136, 0.7);
  15. filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
  16. padding: 10px;
  17. border-radius: 5px;
  18. border: 1px solid #cccccc;
  19. bottom: 20px;
  20. left: 10px;
  21. opacity: 0;
  22. transition: opacity 100ms ease-in;
  23. }
  24. </style>
  25. </head>
  26. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
  27. <div id="map" style="margin:0 auto;width: 100%;height: 100%;"></div>
  28. <div id="popup" class="ol-popup">
  29. <div id="popup-content"></div>
  30. </div>
  31. <script type="text/javascript">
  32. var url = (window.isLocal ? window.server : "https://iserver.supermap.io") +
  33. "/iserver/services/map-mvt-California/rest/maps/California";
  34. getResolutions = function (zoom, scale, targetMinZoom, targetMaxZoom) {
  35. var res = scaleToResolution(scale);
  36. var minRes = res * Math.pow(2, zoom - targetMinZoom);
  37. var resolutions = [];
  38. for (var index = 0; index < targetMaxZoom - targetMinZoom + 1; index++) {
  39. resolutions.push(minRes / Math.pow(2, index));
  40. }
  41. return resolutions;
  42. }
  43. scaleToResolution = function (scale) {
  44. var inchPerMeter = 1 / 0.0254;
  45. var meterPerMapUnitValue = Math.PI * 2 * 6378137 / 360;
  46. return 1 / (scale * 96 * inchPerMeter * meterPerMapUnitValue);
  47. };
  48. var vectorLayer;
  49. //通过缓存sci文件已知第10级的比例尺是0.000003461454994642,计算0到16级的分辨率
  50. var resolutions = getResolutions(10, 0.000003461454994642, 0, 16);
  51. var map = new ol.Map({
  52. target: 'map',
  53. view: new ol.View({
  54. center: [-122.228687503369, 38.1364932162598],
  55. zoom: 10,
  56. minZoom: 10,
  57. maxZoom: 14,
  58. projection: 'EPSG:4326',
  59. resolutions: resolutions
  60. })
  61. });
  62. var container = document.getElementById('popup');
  63. var content = document.getElementById('popup-content');
  64. info = new ol.control.Control({
  65. element: container
  66. });
  67. info.setMap(map);
  68. map.addControl(info);
  69. var format = new ol.format.MVT({
  70. featureClass: ol.Feature
  71. });
  72. // format.defaultDataProjection = new ol.proj.Projection({
  73. // code: 'EPSG:4326',
  74. // units: ol.proj.Units.TILE_PIXELS
  75. // });
  76. var style = new ol.supermap.MapboxStyles({
  77. url: url,
  78. source: 'California',
  79. resolutions: resolutions
  80. })
  81. style.on('styleloaded', function () {
  82. vectorLayer = new ol.layer.VectorTile({
  83. //设置避让参数
  84. declutter: true,
  85. source: new ol.source.VectorTileSuperMapRest({
  86. url: url,
  87. projection: 'EPSG:4326',
  88. tileGrid: new ol.tilegrid.TileGrid({
  89. resolutions: resolutions,
  90. origin: [-180, 90],
  91. tileSize: 512
  92. }),
  93. tileType: 'ScaleXY',
  94. format: format
  95. }),
  96. style: style.getStyleFunction()
  97. });
  98. map.addLayer(vectorLayer);
  99. })
  100. map.on('pointermove', function (e) {
  101. var features = map.getFeaturesAtPixel(e.pixel);
  102. if (!features || features.length === 0) {
  103. content.innerHTML = '';
  104. container.style.opacity = 0;
  105. return;
  106. }
  107. content.innerHTML = "Layer: " + features[0].get('layer') + "<br />" + (features[0].get('NAME') ?
  108. "Name: " + features[0].get('NAME') : "");
  109. container.style.opacity = 1;
  110. });
  111. </script>
  112. </body>
  113. </html>