Динамическая стилизация векторного слоя Стиль в Openlayers без обновления карты - PullRequest
1 голос
/ 20 марта 2019

Я пытаюсь динамически изменить цвет слоя GeoJSON на карте, используя элемент HTML <input type="color">.Мой код работает, но параметр цвета объекта Fill слоя обновляется только при перемещении карты или увеличении / уменьшении масштаба.Есть ли способ сделать это на лету?Я использую функцию setColor () для редактирования значения свойства цвета.

//Change Vector Layer Color 
//Hex to RGB
function hexToRgb(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
}

let layerColorInput1 = document.getElementById('layerColorInput_1')
let layerColorInput2 = document.getElementById('layerColorInput_2')
let layerColorInput3 = document.getElementById('layerColorInput_3')
let layerColorInput4 = document.getElementById('layerColorInput_4')
let layerColorInput5 = document.getElementById('layerColorInput_5')
let layerColorInput6 = document.getElementById('layerColorInput_6')
let layerColorInput7 = document.getElementById('layerColorInput_7')
let layerColorInput8 = document.getElementById('layerColorInput_8')
let layerColorInput9 = document.getElementById('layerColorInput_9')

document.addEventListener("DOMContentLoaded", function (event) {
    layerColorInput1.addEventListener('change', function(){changeLayerColor(5, layerColorInput1)});
});
document.addEventListener("DOMContentLoaded", function (event) {
    layerColorInput2.addEventListener('change', function(){changeLayerColor(6, layerColorInput2)});
});
document.addEventListener("DOMContentLoaded", function (event) {
    layerColorInput3.addEventListener('change', function(){changeLayerColor(7, layerColorInput3)});
});
document.addEventListener("DOMContentLoaded", function (event) {
    layerColorInput4.addEventListener('change', function(){changeLayerColor(8, layerColorInput4)});
});
document.addEventListener("DOMContentLoaded", function (event) {
    layerColorInput5.addEventListener('change', function(){changeLayerColor(9, layerColorInput5)});
});
document.addEventListener("DOMContentLoaded", function (event) {
    layerColorInput6.addEventListener('change', function(){changeLayerColor(10, layerColorInput6)});
});
document.addEventListener("DOMContentLoaded", function (event) {
    layerColorInput7.addEventListener('change', function(){changeLayerColor(11, layerColorInput7)});
});
document.addEventListener("DOMContentLoaded", function (event) {
    layerColorInput8.addEventListener('change', function(){changeLayerColor(12, layerColorInput8)});
});
document.addEventListener("DOMContentLoaded", function (event) {
    layerColorInput9.addEventListener('change', function(){changeLayerColor(13, layerColorInput9)});
});

function changeLayerColor(i, layerColorInput){  
    let newColor = hexToRgb(layerColorInput.value);
    let newColorArray = [newColor.r, newColor.g, newColor.b, 1.00]
    mapLayers[i].style_.fill_.setColor(newColorArray);
}

1 Ответ

1 голос
/ 20 марта 2019

Установка приватных свойств объектов не будет генерировать событие. Используйте документированную функцию API setStyle(), и дисплей обновится.

let style = mapLayers[i].getStyle();
style.getFill().setColor(newColorArray);
mapLayers[i].setStyle(style);



  /**
  * Set the style for features.  This can be a single style object, an array
  * of styles, or a function that takes a feature and resolution and returns
  * an array of styles. If it is `undefined` the default style is used. If
  * it is `null` the layer has no style (a `null` style), so only features
  * that have their own styles will be rendered in the layer. See
  * {@link module:ol/style} for information on the default style.
  * @param {import("../style/Style.js").default|Array<import("../style/Style.js").default>|import("../style/Style.js").StyleFunction|null|undefined} style Layer style.
  * @api
  */
  setStyle(style) {
    this.style_ = style !== undefined ? style : createDefaultStyle;
    this.styleFunction_ = style === null ?
      undefined : toStyleFunction(this.style_);
    this.changed();
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...