Heatmap in Openlayers using an XML (KML formatted) string, styling is incorrect

后端 未结 1 1382
攒了一身酷
攒了一身酷 2021-01-26 05:52

I am currently attempting to create a Heatmap in OpenLayers using a KML string. From this string, I read the features, add them into a VectorSource, then add the source to the H

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-26 06:42

    Thanks to @Mike, the solution was to set the feature styles to undefined. This ensures the pins do not override the styles that are being applied from the Heatmap layer.

    So the new code looks as follows:

            var kmlFeatures = new ol.format.KML().readFeatures(data["xml"],{
                dataProjection : 'EPSG:4326',
                featureProjection : 'EPSG:3857'
              });
    
            var source = new ol.source.Vector({
                features: kmlFeatures,
                format: new ol.format.KML({
                    extractStyles: false
                })
            })
    
            for (var i = 0; i < source.getFeatures().length; i++) {
                var feature = source.getFeatures()[i];
                var name = feature.get('name');
                feature.set('weight', parseFloat(name));
                feature.set('type', "OTHER");
                feature.setStyle(undefined);
            }
    
            var vector = new ol.layer.Heatmap({
                source: source,
                blur: parseInt(15, 10),
                radius: parseInt(5, 10)
              });
    
            map.getMap().addLayer(vector);
    

    The solution feels a bit hacky, but it works.

    0 讨论(0)
提交回复
热议问题