1. Introduction In many cases, the number of features in a point feature layer may be hundreds or thousands. If they are directly loaded onto the map without any processing, not only will the user's visual experience deteriorate, but it will also cause the map interface to freeze. The following code creates <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta charset="utf-8" /> <title>OpenLayers</title> <style> html, body, #map { width: 100%; height: 100%; margin: 0; padding: 0; } </style> <link href="libs/ol/ol.css" rel="stylesheet" /> <script src="libs/ol/ol.js"></script> </head> <body> <div id="map"></div> <script> // Create 1000 random features var source = new ol.source.Vector(); for (var i = 1; i <= 200; i++) { var coordinates = [120.00 + Math.random(), 30.00 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.01 + Math.random(), 30.01 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.02 + Math.random(), 30.02 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.03 + Math.random(), 30.03 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.04 + Math.random(), 30.04 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } // Create a layer var layer = new ol.layer.Vector({ source: source, style: function (feature, resolution) { var style = new ol.style.Style({ image: new ol.style.Icon({ src: 'img/location.png' }) }) return style; } }); // Create a map var map = new ol.Map({ target: 'map', layers: new ol.layer.Tile({ source: new ol.source.OSM() }), layer ], view: new ol.View({ projection: 'EPSG:4326', center: [120, 30], zoom: 10, minZoom: 5, maxZoom: 14 }) }); </script> </body> </html> The running results are shown in the figure below: Doesn’t it feel disgusting to see so many points crowded together? Generally speaking, if there are a large number of points in a point feature layer, we will process it by 2. Aggregation of point feature layers In
The layer aggregation code is as follows: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta charset="utf-8" /> <title>OpenLayers</title> <style> html, body, #map { width: 100%; height: 100%; margin: 0; padding: 0; } </style> <link href="libs/ol/ol.css" rel="stylesheet" /> <script src="libs/ol/ol.js"></script> </head> <body> <div id="map"></div> <script> // Create 1000 random features var source = new ol.source.Vector(); for (var i = 1; i <= 200; i++) { var coordinates = [120.00 + Math.random(), 30.00 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.01 + Math.random(), 30.01 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.02 + Math.random(), 30.02 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.03 + Math.random(), 30.03 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.04 + Math.random(), 30.04 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } // Aggregation var cluster = new ol.source.Cluster({ source: source, distance: 100 }) // Create a layer var layer = new ol.layer.Vector({ source: cluster, style: function (feature, resolution) { var size = feature.get('features').length; var style = new ol.style.Style({ image: new ol.style.Circle({ radius: 30, stroke: new ol.style.Stroke({ color: 'white' }), fill: new ol.style.Fill({ color: 'blue' }) }), text: new ol.style.Text({ text: size.toString(), fill: new ol.style.Fill({ color: 'white' }) }) }) return style; } }); // Create a map var map = new ol.Map({ target: 'map', layers: new ol.layer.Tile({ source: new ol.source.OSM() }), layer ], view: new ol.View({ projection: 'EPSG:4326', center: [120, 30], zoom: 10, minZoom: 5, maxZoom: 14 }) }); </script> </body> </html> The running results are shown in the figure below: 3. Special treatment of polymerization Although the above code realizes the aggregation of point feature layers, there is actually a problem: Generally speaking, <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta charset="utf-8" /> <title>OpenLayers</title> <style> html, body, #map { width: 100%; height: 100%; margin: 0; padding: 0; } </style> <link href="libs/ol/ol.css" rel="stylesheet" /> <script src="libs/ol/ol.js"></script> </head> <body> <div id="map"></div> <script> // Create 1000 random features var source = new ol.source.Vector(); for (var i = 1; i <= 200; i++) { var coordinates = [120.00 + Math.random(), 30.00 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.01 + Math.random(), 30.01 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.02 + Math.random(), 30.02 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.03 + Math.random(), 30.03 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.04 + Math.random(), 30.04 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } // Aggregation var cluster = new ol.source.Cluster({ source: source, distance: 100 }) // Create a layer var layer = new ol.layer.Vector({ source: cluster, style: function (feature, resolution) { var size = feature.get('features').length; if (size == 1) { return new ol.style.Style({ image: new ol.style.Icon({ src: 'img/location.png' }) }) } else { return new ol.style.Style({ image: new ol.style.Circle({ radius: 30, stroke: new ol.style.Stroke({ color: 'white' }), fill: new ol.style.Fill({ color: 'blue' }) }), text: new ol.style.Text({ text: size.toString(), fill: new ol.style.Fill({ color: 'white' }) }) }) } } }); // Create a map var map = new ol.Map({ target: 'map', layers: new ol.layer.Tile({ source: new ol.source.OSM() }), layer ], view: new ol.View({ projection: 'EPSG:4326', center: [120, 30], zoom: 10, minZoom: 5, maxZoom: 14 }) }); </script> </body> </html> The running results are shown in the figure below: In fact, this effect is very simple to implement. The core code is: 4. Special treatment of polymerization 2 In the above code, I set the maximum zoom level of the map to <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta charset="utf-8" /> <title>OpenLayers</title> <style> html, body, #map { width: 100%; height: 100%; margin: 0; padding: 0; } </style> <link href="libs/ol/ol.css" rel="stylesheet" /> <script src="libs/ol/ol.js"></script> </head> <body> <div id="map"></div> <script> // Create 1000 random features var source = new ol.source.Vector(); for (var i = 1; i <= 200; i++) { var coordinates = [120.00 + Math.random(), 30.00 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.01 + Math.random(), 30.01 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.02 + Math.random(), 30.02 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.03 + Math.random(), 30.03 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } for (var i = 1; i <= 200; i++) { var coordinates = [120.04 + Math.random(), 30.04 + Math.random()]; var feature = new ol.Feature(new ol.geom.Point(coordinates)); source.addFeature(feature); } // Aggregation var cluster = new ol.source.Cluster({ source: source, distance: 100 }) // Create a layer var layer = new ol.layer.Vector({ source: cluster, style: function (feature, resolution) { var size = feature.get('features').length; if (size == 1) { return new ol.style.Style({ image: new ol.style.Icon({ src: 'img/location.png' }) }) } else { return new ol.style.Style({ image: new ol.style.Circle({ radius: 30, stroke: new ol.style.Stroke({ color: 'white' }), fill: new ol.style.Fill({ color: 'blue' }) }), text: new ol.style.Text({ text: size.toString(), fill: new ol.style.Fill({ color: 'white' }) }) }) } } }); // Create a map var map = new ol.Map({ target: 'map', layers: new ol.layer.Tile({ source: new ol.source.OSM() }), layer ], view: new ol.View({ projection: 'EPSG:4326', center: [120, 30], zoom: 10, minZoom: 5, maxZoom: 14 }) }); // Listen for map resolution change events map.getView().on('change:resolution', function (event) { if (map.getView().getZoom() == map.getView().getMaxZoom()) { cluster.setDistance(0); } else { cluster.setDistance(100); } }) </script> </body> </html> The running results are shown in the figure below: The implementation of this effect is also very simple. You only need to listen to the resolution change event of the current map. If the current zoom level is already the maximum level, set the aggregation distance to 5. Conclusion When there are a large number of elements, we should consider aggregating them, which not only improves the user experience but also avoids interface freezes. In fact, in the above code, I listened to map.on('moveend', function (event) { if (map.getView().getZoom() == map.getView().getMaxZoom()) { cluster.setDistance(0); } else { cluster.setDistance(100); } }); The same effect can be achieved by listening to the This is the end of this article about how to implement aggregate display of point feature layers in OpenLayers. For more content about aggregate display of point feature layers in OpenLayers, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Solution to Ubuntu 20.04 Firefox cannot play videos (missing flash plug-in)
>>: Super detailed MySQL usage specification sharing
mysql id starts from 1 and increases automaticall...
Table of contents 1. Introduction 2. Initial Vue ...
Preface Today I encountered a very strange proble...
Adding the right VS Code extension to Visual Stud...
I recently configured a server using Tencent Clou...
I have seen many relevant tutorials on the Intern...
Wired network: Ethernet Wireless network: 4G, wif...
This article shares the specific code of React+ts...
Table of contents 1. Object change detection 2. Q...
In the horizontal direction, you can set the alig...
After MySQL 5.7.18 is successfully installed, sin...
Table of contents The principle and function of l...
background Temporary tablespaces are used to mana...
The correspondence between tensorflow version and...
Find the problem Today at work, when copying tabl...