mvtvectorlayer_vectortilerest.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_vectortilerest"></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 styleURL = (window.isLocal ? window.server : "https://iserver.supermap.io") +
  33. "/iserver/services/map-china400/restjsr/v1/vectortile/maps/China_4326/style.json";
  34. //MVT 矢量瓦片第0级分辨率为全球范围宽度除以瓦片宽度512.
  35. //常见坐标系第0级分辨率 WebMercator(3857):2*6378137*Math.PI/512 WGS84(4326):360.0/512 China2000(4490):360.0/512 Beijing54(4214):360.0/512 Xian80(4610):360.0/512
  36. var topResolution = 360.0 / 512;
  37. var resolutions = [];
  38. for (var zoom = 0; zoom < 22; zoom++) {
  39. resolutions.push(topResolution / Math.pow(2, zoom));
  40. }
  41. var vectorLayer;
  42. var map = new ol.Map({
  43. target: 'map',
  44. view: new ol.View({
  45. center: [116, 39],
  46. zoom: 6,
  47. projection: 'EPSG:4326',
  48. resolutions: resolutions
  49. })
  50. });
  51. var container = document.getElementById('popup');
  52. var content = document.getElementById('popup-content');
  53. info = new ol.control.Control({
  54. element: container
  55. });
  56. info.setMap(map);
  57. map.addControl(info);
  58. var format = new ol.format.MVT({
  59. featureClass: ol.Feature
  60. });
  61. var style = new ol.supermap.MapboxStyles({
  62. style: styleURL,
  63. // source: 'China_4326',
  64. map: map
  65. })
  66. style.on('styleloaded', function () {
  67. vectorLayer = new ol.layer.VectorTile({
  68. declutter: true,
  69. source: new ol.source.VectorTileSuperMapRest({
  70. style: styleURL,
  71. projection: 'EPSG:4326',
  72. // source: 'China_4326',
  73. format: format
  74. }),
  75. style: style.getStyleFunction()
  76. });
  77. map.addLayer(vectorLayer);
  78. })
  79. map.on('pointermove', function (e) {
  80. var features = map.getFeaturesAtPixel(e.pixel);
  81. if (!features || features.length === 0) {
  82. content.innerHTML = '';
  83. container.style.opacity = 0;
  84. return;
  85. }
  86. content.innerHTML = "Layer: " + features[0].get('layer') + "<br />" + (features[0].get(
  87. 'NAME') ?
  88. "Name: " + features[0].get('NAME') : "");
  89. container.style.opacity = 1;
  90. });
  91. </script>
  92. </body>
  93. </html>