Javascript 在没有交互的情况下更改对象的样式

Javascript 在没有交互的情况下更改对象的样式,javascript,openlayers-3,Javascript,Openlayers 3,当我在OpenLayers3中选择一个对象时,我想在地图上画一笔,就像你点击地铁站时的这个站点: 为此,我试过: map.on('click', function(evt) { // Delete the overlay if it's not null if (overlay = 'undefined'){map.removeLayer(overlay)}; // Remove popups

当我在OpenLayers3中选择一个对象时,我想在地图上画一笔,就像你点击地铁站时的这个站点:

为此,我试过:

map.on('click', function(evt)
        {
            // Delete the overlay if it's not null

            if (overlay = 'undefined'){map.removeLayer(overlay)};

            // Remove popups
            $(popup).remove();
            popup = null;

            var coord = evt.coordinate;

            var feature = map.forEachFeatureAtPixel(evt.pixel, 
                function(feature, layer) 
                {
                    return feature;
                },null,
                function(layer) 
                {
                    return layer == vector1;
                }
                );

            spawnPopup(coord,feature);
        }
    );

function spawnPopup(coord,feature)
{

    if (feature) {

            var props = feature.getProperties();
            console.log(props);

        }

    var test = null;

    popup = $("<div class='tad_popup'><div id='tad_close_popup' onclick='destroyPopup()'></div><div id='tad_text_popup'><p>Arr&ecirc;t "+  props.num + "</p><p>"+  props.nom + "</p></div></div>");

    var test = $(props.geometry);

    var overlay = new ol.Overlay({
        element:popup
    });

    var overlay_test = new ol.layer.Vector({source: test,
                                            style: new ol.style.Style
                                            ({
                                            image: new ol.style.Circle
                                                    ({
                                                    radius: 5,
                                                    fill: new ol.style.Fill({color: 'rgba(255,100,100,0.9)'}),
                                                    stroke: new ol.style.Stroke ({color: 'rgba(255, 255, 255, 1)',width: 2})
                                                    })})});

    overlay.setPosition(coord);
    map.addOverlay(overlay);
    map.addLayer(overlay_test);
但这不是解决办法

我能为此做些什么

多谢各位


Geo-x

您可以使用交互,然后在构造函数中设置样式属性。或者手动将通过单击事件处理程序中的forEachFeatureAtPixel获得的功能添加到具有自定义样式的中,如本例所示:

这太完美了!我试着使用新的ol.Overlay,而不是新的ol.FeatureOverlay,这就是它不好的原因。谢谢你的帮助。