Openlayers 3分被多边形覆盖

时间:2016-07-05 03:30:28

标签: javascript openlayers-3

var normalstyles = {
    'Point': [new ol.style.Style({
        image: new ol.style.Circle({
            fill: new ol.style.Fill({ 
                color: 'rgba(255,255,255,0)'
            }),
            stroke: new ol.style.Stroke({ 
                color: 'rgba(0,0,0,1)'
            }),
            radius: 5
        })
    })],
    'Polygon': [new ol.style.Style({
        fill: new ol.style.Fill({
            color: 'rgba(255,255,255,0)'
        }),
        stroke: new ol.style.Stroke({
            width: 1,
            color: 'rgba(255,255,0,0)'
        })
    })]
};

var geoJSON = new ol.layer.Image({
    title: 'buildings',
    source: new ol.source.ImageVector({
        source: new ol.source.Vector({
            url: 'data/geojson',
            format: new ol.format.GeoJSON({
                defaultDataProjection :'EPSG:4326', 
                projection: 'EPSG:3857'
            })
        }),
        style: function(feature, resolution){
            var geom = feature.getGeometry().getType();
            return normalstyles[geom];
        }      
    })
});

var highlightstyles = {
    'Point': [new ol.style.Style({
        image: new ol.style.Circle({
            fill: new ol.style.Fill({ 
                color: 'rgba(255,0,0,0.1)'
            }),
            stroke: new ol.style.Stroke({ 
                color: '#f00'
            }),
            radius: 5
        })
    })],
    'Polygon': [new ol.style.Style({
        fill: new ol.style.Fill({
            color: 'rgba(255,0,0,0.1)'
        }),
        stroke: new ol.style.Stroke({
            color: '#f00',
            width: 1
        })
    })]
};

var map = new ol.Map({      
    layers: [new ol.layer.Tile({ source: new ol.source.OSM()}), geoJSON],
    target: document.getElementById('map'),
    view: new ol.View({
        center: center,
        zoom: 15
    })
});

var featureOverlay = new ol.layer.Vector({
    source: new ol.source.Vector(),
    map: map,
    style: function(feature, resolution){
        var geom = feature.getGeometry().getType();
        return highlightstyles[geom];
    }
}); 

var highlight;
var displayFeatureInfo = function(pixel) {

    var feature = map.forEachFeatureAtPixel(pixel, function(feature) {
      return feature;
    });

    if (feature !== highlight) {
        if (highlight) {
            featureOverlay.getSource().removeFeature(highlight);
        }
        if (feature) {
            featureOverlay.getSource().addFeature(feature);
        }
        highlight = feature;
    }

};

这是我的代码。它基本上显示了geojson中的信息,并为鼠标悬停效果提供了一个亮点。 [This is what it looks like when it is idle] [This is what it looks like when I mouseover the polygon]

对于多边形之外的点,当鼠标悬停在表示该点的圆上时,我可以获得高光效果。但是,无法达到多边形内部的那些点。它们被多边形覆盖。

如何让这些圆圈代表多边形未覆盖的点?

更新

现在我已经将我的代码更改为此,并且积分正在获得亮点。但是,多边形不再能够显示高光。有什么想法吗?

var displayFeatureInfo = function(pixel) {

    var featurePoint = map.forEachFeatureAtPixel(pixel, function(feature) {
        if (feature.getGeometry().getType() != 'Point') return feature;
    });

    var featurePolygon = map.forEachFeatureAtPixel(pixel, function(feature) {
        if (feature.getGeometry().getType() != 'Polygon') return feature;
    });

    if (featurePoint !== highlight) {
        if (highlight) {
            featureOverlay.getSource().removeFeature(highlight);
        }
        if (featurePoint) {
            featureOverlay.getSource().addFeature(featurePoint);
        }
        highlight = featurePoint;
    }

    if (featurePolygon !== highlight) {
        if (highlight) {
            featureOverlay.getSource().removeFeature(highlight);
        }
        if (featurePolygon) {
            featureOverlay.getSource().addFeature(featurePolygon);
        }
        highlight = featurePolygon;
    }

};

1 个答案:

答案 0 :(得分:0)

我能想到的最好的方法是将您的功能放在不同的层中,一个用于点,另一个用于多边形,这样在您的

var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
//here you can add a condition on layer
  return feature;
});

或者如果绝对有必要将它们保存在一个图层中,您可以检查几何类型

var featurePoint = map.forEachFeatureAtPixel(pixel, function(feature) {
    if(feature.getGeometry().getType()!="Point"){
        return feature;
    }
});
var featurePolygon = map.forEachFeatureAtPixel(pixel, function(feature) {
    if(feature.getGeometry().getType()!="Polygon"){
       return feature;
    }
});