使用功能使openlayers中图标的颜色变化可见

问题描述 投票:1回答:1

我试图实现将标记图标的颜色更改为在地图上可见的功能。它们在地图上以不同的颜色显示。这些颜色与json cat_id键的颜色相对应。

1: "http://dev.openlayers.org/img/marker.png",
2: "http://dev.openlayers.org/img/marker-blue.png",
3: "http://dev.openlayers.org/img/marker-gold.png",
4: "http://dev.openlayers.org/img/marker-green.png",

地图中的每个标记图标正在从script标签中数据ID中的json对象获取其位置,颜色和其他有价值的数据。我没有编写与openlayer libary紧密相关的部分代码。我写了脚本总数的以下部分:

var json = document.getElementById('data').innerHTML;
json = JSON.parse(json); // converts text into JSON

// search criteria (could be changed)
var $search_postal = "8912JB";
var $search_number = "10";
var $change_cat = 1;

function changeColor() {
    for (var key in json) {
        if (json.hasOwnProperty(key)) {
            if (json[key].postal == $search_postal) {
                if (json[key].number == $search_number) {
                    alert("Gonna change cat_id " + json[key].cat_id + " of the number search string " + $search_number + " of the postal search string " + $search_postal + " to cat_id " + $change_cat);
                    json[key].cat_id = "1";
                    alert('Changed!');
                    const posts = json; // the constant that is used to display the icons
                    var myJSON = json;
                    document.getElementById('demo').innerHTML = JSON.stringify(myJSON, undefined, 2); // changes of the json data to see on screen
                }
            }
        }
    }
}

const posts = json; //the constant that is used to display the icons

var myJSON = JSON.stringify(json);
document.getElementById('demo').innerHTML = myJSON;

此脚本将更改给定搜索输入的cat_id。在该示例中,它搜索编号为1的邮政8912JB。一旦找到要定位的位置,它将cat_id值3更改为1。换句话说,json数据已更改。由于它存储在const帖子中,因此我用已更改的数据替换了这个缺点。我的问题是我无法使用已更改的新json数据(cat_id:邮政8912JB号10的1)来刷新地图中的标记图标。这与对Openlayers和Javascript的了解较少有关。我已经为此挣扎了两个星期。有人可以帮我实现这一目标吗?如果颜色发生变化,则唯一需要的就是它。我只需要对变量$ search_postal,$ search_number和$ change_cat进行更改。

这是调试页面:https://jsfiddle.net/j4z1vpht/4/

感谢您的帮助,>>

网格

我试图实现将标记图标的颜色更改为在地图上可见的功能。它们在地图上以不同的颜色显示。这些颜色对应于json cat_id键'...

javascript jquery maps openlayers
1个回答
0
投票

您需要将新类别更新为地图特征,如下所示:

© www.soinside.com 2019 - 2024. All rights reserved.