Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

javascript - 通过功能使图标的颜色变化在openlayers中可见(Make changes of colors of icons visible in openlayers with a function)

I try to accomplish that I will be able to change the color of marker icons to be visible in the map.

(我试图做到,我将能够更改标记图标的颜色以使其在地图上可见。)

They are in the map in different colors.

(它们在地图上以不同的颜色显示。)

Those colors are correspondenting to the json cat_id key's.

(这些颜色与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",

Each marker icon that is in the map is getting it's position, color and other valuable data from the json objects in the data id in the script tag.

(地图中的每个标记图标都将从script标记的数据ID中的json对象获取其位置,颜色和其他有价值的数据。)

I didn't code the part of that work's close with the openlayer libary's.

(我没有编写与openlayer libary紧密相关的部分代码。)

I wrote the following part of the total of the script:

(我在脚本总数中写了以下部分:)

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;

This script will change the cat_id of the given search input.

(该脚本将更改给定搜索输入的cat_id。)

In the example it does a search for postal 8912JB with number 1. Once it has made the loop to find it's position it will change the cat_id value of 3 to 1. In other words the json data has been changed.

(在此示例中,它搜索编号为1的邮政8912JB。一旦找到要定位的位置,它将cat_id值3更改为1。换句话说,json数据已更改。)

Since it is stored in the const posts I replace this cons with the changed data.

(由于它存储在const帖子中,因此我用更改后的数据替换了这个缺点。)

My problem is that I'm unable to refresh the marker icon(s) in the map with the new json data that is changed (cat_id: 1 of postal 8912JB number 10).

(我的问题是我无法使用更改后的新json数据(cat_id:邮政8912JB号10的1)来刷新地图中的标记图标。)

This has to do with less knowledge about openlayers and Javascript.

(这与对openlayers和Javascript的了解较少有关。)

I'm struggling already 2 weeks with this.

(我已经为此挣扎了两个星期。)

Can anybody help me to accomplish this?

(有人可以帮我实现这一目标吗?)

If it changes from color is the only thing that is required.

(如果颜色发生变化,则唯一需要的就是它。)

I would only need to give the changes to the variables $search_postal, $search_number and $change_cat.

(我只需要对变量$ search_postal,$ search_number和$ change_cat进行更改即可。)

Here is the page for debug: https://jsfiddle.net/j4z1vpht/4/

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

Thanks for any help,

(谢谢你的帮助,)

grid

(格线)

  ask by grid translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...