mvtVectorLayer4326.html 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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_mvtVectorLayer4326"></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. </head>
  12. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
  13. <div id="map" style="margin:0 auto;width: 100%;height: 100%;"></div>
  14. <script type="text/javascript">
  15. var url = (window.isLocal ? window.server : "https://iserver.supermap.io") + "/iserver/services/map-world/rest/maps/World";
  16. new ol.supermap.MapService(url).getMapInfo(function (serviceResult) {
  17. var map = new ol.Map({
  18. target: 'map',
  19. controls: ol.control.defaults({attributionOptions: {collapsed: false}})
  20. .extend([new ol.supermap.control.Logo()]),
  21. view: new ol.View({
  22. center: [0, 0],
  23. zoom: 2,
  24. projection: 'EPSG:4326'
  25. }),
  26. });
  27. var stylesOptions = {
  28. url: url,
  29. view: map.getView()
  30. }
  31. var vectorTileStyles = new ol.supermap.VectorTileStyles(stylesOptions);
  32. var vectorTileOptions = ol.source.VectorTileSuperMapRest.optionsFromMapJSON(url, serviceResult.result);
  33. vectorTileOptions.format = new ol.format.MVT({featureClass: ol.Feature});
  34. vectorTileOptions.format.dataProjection = new ol.proj.Projection({
  35. code: 'EPSG:4326',
  36. units: ol.proj.Units.TILE_PIXELS
  37. });
  38. var vectorLayer = new ol.layer.VectorTile({
  39. //设置避让参数
  40. declutter: true,
  41. source: new ol.source.VectorTileSuperMapRest(vectorTileOptions),
  42. style: vectorTileStyles.getFeatureStyle
  43. });
  44. map.addLayer(vectorLayer);
  45. map.on('click', function (e) {
  46. map.forEachFeatureAtPixel(e.pixel, function (feature) {
  47. vectorTileStyles.dispatchEvent({
  48. type: 'featureSelected',
  49. selectedId: feature.getId(),
  50. layerName: feature.getProperties().layer
  51. });
  52. return true;
  53. }, {hitTolerance: 5});
  54. vectorLayer.changed();
  55. })
  56. });
  57. </script>
  58. </body>
  59. </html>