mvtVectorLayer.html 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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" 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-china400/rest/maps/China";
  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: [12957388, 4853991],
  23. zoom: 11,
  24. multiWorld: true
  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. var vectorLayer = new ol.layer.VectorTile({
  35. //设置避让参数
  36. declutter: true,
  37. source: new ol.source.VectorTileSuperMapRest(vectorTileOptions),
  38. style: vectorTileStyles.getFeatureStyle
  39. });
  40. map.addLayer(vectorLayer);
  41. map.on('click', function (e) {
  42. map.forEachFeatureAtPixel(e.pixel, function (feature) {
  43. vectorTileStyles.dispatchEvent({type: 'featureSelected',
  44. selectedId: feature.getId(),
  45. layerName: feature.getProperties().layer
  46. });
  47. return true;
  48. }, {hitTolerance: 5});
  49. vectorLayer.changed();
  50. })
  51. });
  52. </script>
  53. </body>
  54. </html>