打开图层中的组标记3。

时间:2017-09-25 15:35:36

标签: javascript openlayers openlayers-3

我想在Open Layer 3中对标记进行分组。像这样:

enter image description here

我在互联网上寻找一些例子,但我找不到它。 如何使用Open Layers 3进行操作?

1 个答案:

答案 0 :(得分:3)

你有一些例子可以推动你朝着正确的方向前进,对于你可以看到的初学者来说:

  1. OpenLayers官方示例:Clustered Features

  2. OpenLayers扩展程序:Animated Cluster

  3. OpenLayers扩展程序:Convex Hull

  4. OpenLayers示例(OpenLayers 3初学者指南):Cluster GeoJSON Source

  5. 还有这个小片段;

    
    
    var map = new ol.Map({
        view: new ol.View({
            zoom: 4,
            center: [2152466, 5850795]
        }),
        target: 'js-map',
        layers: [
            new ol.layer.Tile({
                source: new ol.source.OSM()
            })
        ]
    });
    
    // generate random elements
    var getRandomInt = function(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    };
    
    
    var features = [];
    var numberOfFeatures = 0;
    
    while (numberOfFeatures < 100) {
        var point = new ol.geom.Point([
            getRandomInt(1545862, 2568284),
            getRandomInt(6102732, 7154505)
        ]);
    
        features.push(new ol.Feature(point));
        numberOfFeatures++;
    }
    
    
    var getStyle = function(feature) {
    
        var length = feature.get('features').length;
        return [
            new ol.style.Style({
    
                image: new ol.style.Circle({
                    radius: Math.min(
                        Math.max(length * 0.8, 10), 15 
                    ),
                    fill: new ol.style.Fill({
                        color: [0, 204, 0, 0.6]
                    })
                }),
                text: new ol.style.Text({
                    text: length.toString(),
                    fill: new ol.style.Fill({
                        color: 'black'
                    })
                }),
                stroke: new ol.style.Stroke({
                    color: [0, 51, 0, 1],
                    width: 1
                }),
                font: '26px "Helvetica Neue", Arial'
            })
        ];
    };
    // https://github.com/Viglino/OL3-AnimatedCluster
    var clusterSource = new ol.source.Cluster({
        distance: 100,
        source: new ol.source.Vector({
            features: features 
        })
    });
    
    // Animated cluster layer
    var clusterLayer = new ol.layer.AnimatedCluster({
        source: clusterSource,
        // Use a style function for cluster symbolisation
        style: getStyle
    });
    
    
    map.addLayer(clusterLayer);
    &#13;
    .map {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        font-family: Arial, sans-serif;
    }
    
    .ol-attribution > ul {
        font-size: 1rem;
    }
    &#13;
    <!doctype html>
    <html>
    
    <head>
        <meta charset="utf-8">
        <title>Clusters</title>
        <link rel="stylesheet" href="https://openlayers.org/en/v4.3.3/css/ol.css">
        <link rel="stylesheet" href="style.css">
    </head>
    
    <body>
        <div id="js-map" class="map"></div>
        <script src="https://openlayers.org/en/v4.3.2/build/ol.js"></script>
        <!--animated cluster plugin -->
        <script type="text/javascript" src="https://cdn.rawgit.com/Viglino/OL3-AnimatedCluster/gh-pages/interaction/selectclusterinteraction.js"></script>
        <script type="text/javascript" src="https://cdn.rawgit.com/Viglino/OL3-AnimatedCluster/gh-pages/layer/animatedclusterlayer.js"></script>
        <script src="clustering.js"></script>
    </body>
    
    </html>
    &#13;
    &#13;
    &#13;