07_Cluster.html 3.8 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_Cluster"></title>
  9. <script type="text/javascript" src="../js/include-web.js"></script>
  10. <!-- 此范例基于 openlayers@4.6.5 版本 -->
  11. <script type="text/javascript" include="animatedclusterlayer,ol@4.6.5" src="../../dist/ol/include-ol.js"></script>
  12. </head>
  13. <body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
  14. <div id="map" style="width: 100%;height:100%"></div>
  15. <script type="text/javascript">
  16. var url = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/map-china400/rest/maps/China_4326";
  17. new ol.supermap.MapService(url).getMapInfo(function (serviceResult) {
  18. var mapJSONObj = serviceResult.result;
  19. var map = new ol.Map({
  20. target: 'map',
  21. controls: ol.control.defaults({attributionOptions: {collapsed: false}})
  22. .extend([new ol.supermap.control.Logo()]),
  23. view: new ol.View({
  24. center: [103, 30],
  25. zoom: 4,
  26. projection: 'EPSG:4326'
  27. })
  28. });
  29. var options = ol.source.TileSuperMapRest.optionsFromMapJSON(url, mapJSONObj);
  30. var layer = new ol.layer.Tile({
  31. source: new ol.source.TileSuperMapRest(options)
  32. });
  33. map.addLayer(layer);
  34. var clusterSource = new ol.source.Cluster({
  35. distance: 40,
  36. source: new ol.source.Vector(),
  37. wrapX: false
  38. });
  39. var clusterLayer = new ol.layer.AnimatedCluster({
  40. name: 'Cluster',
  41. source: clusterSource,
  42. animationDuration: 700,
  43. style: getStyle
  44. });
  45. map.addLayer(clusterLayer);
  46. function addFeatures(nb) {
  47. var features = [];
  48. var xmax = 130, xmin = 80, ymax = 50, ymin = 20;
  49. for (var i = 0; i < nb; ++i) {
  50. features[i] = new ol.Feature(new ol.geom.Point([Math.floor(Math.random() * (xmax - xmin + 1) + xmin), Math.floor(Math.random() * (ymax - ymin + 1) + ymin)]));
  51. features[i].set('id', i);
  52. }
  53. clusterSource.getSource().clear();
  54. clusterSource.getSource().addFeatures(features);
  55. }
  56. addFeatures(2000);
  57. });
  58. function getStyle(feature) {
  59. var styleCache = {};
  60. var size = feature.get('features').length;
  61. var style = styleCache[size];
  62. if (!style) {
  63. var color = size > 25 ? "192,0,0" : size > 8 ? "255,128,0" : "0,128,0";
  64. var radius = Math.max(8, Math.min(size * 0.75, 20));
  65. var dash = 2 * Math.PI * radius / 6;
  66. dash = [0, dash, dash, dash, dash, dash, dash];
  67. style = styleCache[size] = [new ol.style.Style({
  68. image: new ol.style.Circle({
  69. radius: radius,
  70. stroke: new ol.style.Stroke({
  71. color: "rgba(" + color + ",0.5)",
  72. width: 15,
  73. lineDash: dash,
  74. lineCap: "butt"
  75. }),
  76. fill: new ol.style.Fill({
  77. color: "rgba(" + color + ",1)"
  78. })
  79. }),
  80. text: new ol.style.Text({
  81. text: size.toString(),
  82. fill: new ol.style.Fill({
  83. color: '#fff'
  84. })
  85. })
  86. })
  87. ];
  88. }
  89. return style;
  90. }
  91. </script>
  92. </body>
  93. </html>