为什么我的for循环跳过一些标记坐标?

时间:2017-10-17 09:04:13

标签: javascript arrays google-maps for-loop google-maps-api-3

我有for loop循环数组(4级深)来检索我的标记位置的纬度和长坐标。它似乎工作正常,但它跳过第5,第8和第9坐标。我真的很感激任何帮助或建议。谢谢。

  var bounds = new google.maps.LatLngBounds();
  for (i=0; i < categories.length; i++){
   for (j=0; j < categories[i].length; j++){
     for (var k = 0; k < categories[i][j].length; k++) {
       marker = new google.maps.Marker({
      position: new google.maps.LatLng(parseFloat(categories[i][1][j][k]), 
      parseFloat(categories[i][2][j][k])),
      index:k,
      icon : categories[i][5][j][0]
    });
    //create marker categories
    marker.category = categories[i][6][j][0];//category[6][j][0]
    marker.setVisible(true);
    markers.push(marker);
    bounds.extend(marker.getPosition());
    marker.setMap(map);
   }
  }
 }


 //the arrays
 var  categories = [category];
 var category = [
 names,
 lat_locations,
 long_locations,
 links,
 icons,
 icon_image,
 real_names,
];

  var lat_locations = [
  container_yards_locations_lats,
  truck_yards_locations_lats,
  terminals_locations_lats,
  new_developments_locations_lats,
  manufacturing_companies_locations_lats,
  warehouses_locations_lats,
  transport_companies_locations_lats,
  jettys_locations_lats,
  free_land_locations_lats
  ];

 var container_yards_locations_lats = [
 6.445638,
 6.447416
 ];

 var truck_yards_locations_lats = [
 6.452937
 ];

 var terminals_locations_lats = [
 6.4356407,
 6.432294,
 6.4433638,
 6.442223,
 6.4371918,
 6.4331848,
 6.433211,
 6.440850
];

 var new_developments_locations_lats = [
 6.450393
 ];

 var manufacturing_companies_locations_lats = [
 6.447272
 ];

 var warehouses_locations_lats = [
 6.446857,
 6.450077
 ];

 var transport_companies_locations_lats = [
 6.450675
 ];

 var jettys_locations_lats = [
 6.443349
 ];

 var free_land_locations_lats = [
 6.451843
 ];

 var lat_locations = [
 container_yards_locations_lats,
 truck_yards_locations_lats,
 terminals_locations_lats,
 new_developments_locations_lats,
 manufacturing_companies_locations_lats,
 warehouses_locations_lats,
 transport_companies_locations_lats,
 jettys_locations_lats,
 free_land_locations_lats
];


var container_yards_locations_lngs = [
 3.3111681,
 3.3113029
];

var truck_yards_locations_lngs = [
 3.3062503
];

var terminals_locations_lngs = [
  3.3330524,
  3.3555287
];

 var   new_developments_locations_lngs = [
  3.3031123
 ];

 var manufacturing_companies_locations_lngs = [
  3.3099291
 ];

 var warehouses_locations_lngs = [
  3.3082508,
  3.3089833
 ];

 var transport_companies_locations_lngs = [
  3.3090743
 ];

 var jettys_locations_lngs = [
  3.3159546
 ];

 var free_land_locations_lngs = [
  3.3017833
];

var long_locations = [
 container_yards_locations_lngs,
 truck_yards_locations_lngs,
 terminals_locations_lngs,
 new_developments_locations_lngs,
 manufacturing_companies_locations_lngs,
 warehouses_locations_lngs,
 transport_companies_locations_lngs,
 jettys_locations_lngs,
 free_land_locations_lngs
];

我觉得将所有数组放在这里太多了,那些数组显然不完整,但这就是解释:

  

var categories = [category];

     

var category = [//包含9个不同的类别];

     

var“sub_category”= [//包含多个变量的数组   每个];

每个类别都有一个包含各种变量的数组。所以要访问 你需要引用最深的变量:

  

类别[0] [2] [8] [0]

1 个答案:

答案 0 :(得分:0)

for循环中的引用都是错误的:

var bounds = new google.maps.LatLngBounds();
  for (i=0; i < categories.length; i++){
   for (j=0; j < categories[0][i].length; j++){
     for (var k = 0; k < categories[0][i][j].length; k++) {
       marker = new google.maps.Marker({
      position: new google.maps.LatLng(parseFloat(categories[i][1][j][k]), 
      parseFloat(categories[i][2][j][k])),
      index:k,
      icon : categories[i][5][j][0]
    });
    //create marker categories
    marker.category = categories[i][6][j][0];//category[6][j][0]
    marker.setVisible(true);
    markers.push(marker);
    bounds.extend(marker.getPosition());
    marker.setMap(map);
   }
  }
 }

现在代码工作正常。