在For循环中将所有JSON数据加载到标记层中时遇到问题

时间:2015-09-04 22:42:36

标签: javascript arcgis esri arcgis-js-api

我有一个类似下面的代码,假设将超过600个点添加到GraphicLayer projectsG中并最终将projectsG添加到layer1map但我不是

确定我做错了什么
projectsG = new esri.Graphic(project, symbol).setInfoTemplate(projectInfoTemplate);
}
console.log(thepoints);
layer1 = new esri.layers.GraphicsLayer();
for (i = 0; i < points.length; ++i) {
    layer1.add(projectsG);
}

仅将最后项目添加到地图中的部分!您能否请看下面的代码,让我知道我在循环中做错了什么?

  var layer1, 
  var thepoints=[];
  var projectInfoTemplate;
  var projectsG;

var points = [
   {
      "ProjectID":"15260",
      "Longitude":"-118.641508",
      "Latitude":"51.949915",
   },
   {
      "ProjectID":"17043",
      "Longitude":"-125.444557",
      "Latitude":"51.097552",
   },
   ....
      {
      "ProjectID":"13009",
      "Longitude":"-130.257086",
      "Latitude":"56.882834",
   },
   {
      "ProjectID":"17088",
      "Longitude":"-124.160699",
      "Latitude":"50.897618",
   }
];

function drawSeries1() {
for (var i = 0; i < points.length; i++) {
    var projects = points[i];
    var project = new esri.geometry.Point(projects.Longitude, projects.Latitude);                  
    project = esri.geometry.geographicToWebMercator(project);

    var symbol = new esri.symbol.PictureMarkerSymbol("e-Chartreuse.png", 32, 32);
    projectInfoTemplate = new InfoTemplate(); 
    projectInfoTemplate.setTitle("Project Details");
    projectInfoTemplate.setContent('<div>Some Att Here</div> ');

    projectsG = new esri.Graphic(project, symbol).setInfoTemplate(projectInfoTemplate);
    thepoints.push(projectsG);
}
console.log(thepoints);
layer1 = new esri.layers.GraphicsLayer();
for (i = 0; i < points.length; ++i) {
    layer1.add(projectsG);
}
map.addLayer(layer1);           


}

1 个答案:

答案 0 :(得分:0)

试试这个循环:

for (var i = 0; i < thepoints.length; ++i) {
  layer1.add(thepoints[i]);
}

甚至&#34;更好&#34;,您可以跳过最后一个for循环,并将thepoints.push(projectsG);替换为前一个for循环中的layer1.add(projectsG);