简体   繁体   English

更改JavaScript地图中的图钉颜色?

[英]Change pin color in javascript map?

We're using an amcharts map, and so far, it all looks pretty good. 我们正在使用一个amcharts地图,到目前为止,一切看起来都还不错。

What I'm trying to do is change the color of the pins based on some value coming from our json stream. 我想做的就是根据来自json流的一些值来更改引脚的颜色。

javascript : javascript

AmCharts.makeChart("mapdiv", {
"type": "map",
"theme": "light",
"imagesSettings": {
  "rollOverColor": "#089282",
  "rollOverScale": 1,
  "selectedScale": 0.5,
  "selectedColor": "#089282",
  "color": "#13564e",
  "selectable": false,
  "bringForwardOnHover": false
},
"areasSettings": {
  "color": "#D3D3D3",
"autoZoom": true
},  
"data": {
  "map": "puertoRicoHigh",
  "getAreasFromMap": true
},
"dataLoader": {
  "url": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/716619/30189.json",
  "format": "json",
  "showErrors": true,
  "postProcess": function(data, config, map) {
    // create a new dataProvider
    var mapData = map.data;

    // init images array
    if (mapData.images === undefined)
      mapData.images = [];

    // create images out of loaded data
    for(var i = 0; i < data.length; i++) {
      var image = data[i];
      image.type = "circle";
      mapData.images.push(image);
    }
    return mapData;
  }
}

}); });

That works well, but I would like to change the pins being displayed in the map. 效果很好,但我想更改地图上显示的图钉。 Ideally, I would like to use images (jpg, png, etc) that I've downloaded from the web. 理想情况下,我想使用从网上下载的图像(jpg,png等)。

So let's say that my json looks like this: 因此,假设我的json如下所示:

[{"title":"Site1","longitude":18.4262,"latitude":-67.1483,"inservice":true},
{"title":"Site2","longitude":18.3663,"latitude":-66.1887,"inservice":false}]

How can I change the pin color/size/font (or even image) so that if "inservice" is true, then use one pin. 如何更改图钉的颜色/大小/字体(甚至图像),以便如果“ inservice”为true,则使用一个图钉。 If it's false, then use another pin. 如果为假,则使用另一个引脚。

I tried using the SO snippet tool, but it's not rendering the map. 我尝试使用SO摘录工具,但未渲染地图。 So here it is, in jsfiddle: https://jsfiddle.net/wn61bfqb/ 在jsfiddle中就是这样: https ://jsfiddle.net/wn61bfqb/

  AmCharts.makeChart("mapdiv", { "type": "map", "theme": "light", "imagesSettings": { "rollOverColor": "#089282", "rollOverScale": 1, "selectedScale": 0.5, "selectedColor": "#089282", "color": "#13564e", "selectable": false, "bringForwardOnHover": false }, "areasSettings": { "color": "#D3D3D3", "autoZoom": true }, "data": { "map": "puertoRicoHigh", "getAreasFromMap": true }, "dataLoader": { "url": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/716619/30189.json", "format": "json", "showErrors": true, "postProcess": function(data, config, map) { // create a new dataProvider var mapData = map.data; // init images array if (mapData.images === undefined) mapData.images = []; // create images out of loaded data for(var i = 0; i < data.length; i++) { var image = data[i]; image.type = "circle"; mapData.images.push(image); } return mapData; } } }); 
 #mapdiv { width: 100%; height: 300px; 
 <script src="https://www.amcharts.com/lib/3/ammap.js"></script> <script src="https://www.amcharts.com/lib/3/maps/js/puertoRicoHigh.js"></script> <script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script> <link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" /> <script src="https://www.amcharts.com/lib/3/themes/light.js"></script> <script src="https://www.amcharts.com/lib/3/plugins/dataloader/dataloader.js"></script> <div id="mapdiv"></div> 

Thanks. 谢谢。

inside the dataLoader function where your are looping through the datasets add a check if inservice is true. 在您要遍历数据集的dataLoader函数内部,添加一个检查inservice是否为true。 Based on this check add a color. 根据此检查添加颜色。

  for (var i = 0; i < data.length; i++) {
    var image = data[i];
    image.type = "circle";
  if(data[i].inservice){
     image.color = 'green'; // if inservice is true
  } else {
     image.color = 'red'; // if inservice is false or undefined 
  }
    mapData.images.push(image);
  }

I noticed that the end point you are calling to create this map is not returning inservice field yet. 我注意到,终点你打电话来创建这个地图没有返回inservice场呢。 So till then we end up showing red pin all the time. 因此,直到那时我们一直都显示红色图钉。

jsFiddle link jsFiddle链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM