如何通过单击来重绘(更改背景色)kml地标?

时间:2019-02-19 10:27:14

标签: javascript openlayers openlayers-3

我有一个页面,显示来自KML的正方形网格。

enter image description here

https://saxrub.net/GeoPortail/500x500m.kml

用户可以选择/取消选择网格,选择内容将根据网格的颜色具体化。 如果他单击红色的网格,它将变为绿色,反之亦然。

KML已添加

var KML3 = new ol.layer.Vector ({ 
  source: new ol.source.Vector ({ 
      url: kml_1x1km, 
         format: new ol.format.KML ({ 
             extractStyles: true, 
             showPointNames: showPointNames 
        }) 
    }) 
 }); 
 allLayers.push (KML3); 
 allLayerSwitcher.push ({ 
    layer: KML3, 
    config: { 
       title: kml_1x1km_title, 
       description: kml_1x1km_title, 
    } 
 })

通过以下方式捕获对地标的点击:

 var displayFeatureInfo = function (pixel) { 
     var currentZoom = map.getView (). getZoom (); 
     var feature = map.forEachFeatureAtPixel (pixel, function (feature, layer) { 
        return feature;
   }); 

 if (feature) { 
    var f = feature; 
    var att = feature.getProperties (); 
    styleUrl = att.styleUrl.split ("#"); 
    mesh=att.description.split = ( "~"); 
    if (styleUrl [1] == "Red_red") 
        { 
             att.styleUrl = "# Jaune_Vert"; 
             feature.style = eval (mesh [3]). getStyle ("#Yellow_Green",   {}) 
         } 
    else 
         { 
               att.styleUrl = "# Rouge_Rouge"; 
               feature.style = eval (mesh [3]). getStyleFunction ("# Red_Red", {}) 
      `   } 
 eval (mesh [3]) redraw (). 
 this.unselect (feature); 
 return 1; 
 } else { 
 return 0; 
 } 
}; 

我正在使用API​​的OpenLayer 2

  if (f) { 

  mesh=f.attributes.description.split = ( "~"); 

  if (f.attributes.styleUrl == "# Red_Red") 
     { 
         f.attributes.styleUrl = "# Jaune_Vert"; 
         f.style = eval (mesh [3]). protocol.format.getStyle ("#Yellow_Yellow", {}) 
     } 
   else 
     { 
         f.attributes.styleUrl = "# Rouge_Rouge"; 
         f.style = eval (mesh [3]). protocol.format.getStyle ("# Red_red", {}) 
     } 
eval (mesh [3]) redraw (). 
 this.unselect (f); 
 } 

但是我找不到V4中的重绘功能。

一个主意?

1 个答案:

答案 0 :(得分:0)

您当前在所选功能上设置样式的方式将无济于事,因为ol/Feature没有style属性。您必须使用setStyle()设置器功能:

feature.setStyle(yourStyle);

渲染器将收到有关此更改的通知,因此您无需在功能上调用setStyle()后手动重绘任何内容。