动态地将数据推送到循环内的对象

时间:2016-09-25 13:39:02

标签: javascript jquery google-maps loops object

我有一个"地点"这是在循环中动态构建的:

for(i = 0; i < railway_s.result.length; i++) {

            station = railway_s.result[i];

            $("#railway_s_ul").append('<li class="blk"><h2 class="org_name">' + station.stationname + '<a href="http://maps.google.com/maps?q='+ station.latlong.coordinates[1] +','+ station.latlong.coordinates[0] +'" class="btn btn-primary" target="_blank">view on map</a><span class="org_id">Station Code: ' + station.crscode + '</span></h2><div class="distance">Distance: ' + distance + ' miles</div></li>')
}

我在循环中的每个地点的坐标都是station.latlong.coordinates[1]station.latlong.coordinates[0]

现在我试图展示每个&#34;地点&#34;在地图上使用每个地方lat和long坐标

我已准备好地图,我有循环,列表工作正常,但我仍然坚持使用代码在同一个Google地图上显示每个地方...

我试过这个(手动输入一些坐标)并且工作正常......

var locations = [
   ['Bondi Beach', -33.890542, 151.274856, 4],
   ['Coogee Beach', -33.923036, 151.259052, 5],
   ['Cronulla Beach', -34.028249, 151.157507, 3],
   ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
   ['Maroubra Beach', -33.950198, 151.259302, 1]
];

var map = new google.maps.Map(document.getElementById('map'), {
   zoom: 10,
   center: new google.maps.LatLng(-33.92, 151.25),
   mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {  
    marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
});

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
  infowindow.setContent(locations[i][0]);
  infowindow.open(map, marker);
}
})(marker, i));
}

我努力将纬度和经度放在&#34; var位置&#34;对象以及循环...有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

使用each迭代位置数组。

var locations = [
   ['Bondi Beach', -33.890542, 151.274856, 4],
   ['Coogee Beach', -33.923036, 151.259052, 5],
   ['Cronulla Beach', -34.028249, 151.157507, 3],
   ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
   ['Maroubra Beach', -33.950198, 151.259302, 1]
];

$.each(locations, function(key, value){
  console.log("Lat: "+ value[1] + " Long: "+ value[2]);
  console.log(); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:1)

在动态创建工作站数据的循环中,将相同的数据添加到位置数组

var locations = [];

for(i = 0; i < railway_s.result.length; i++) {

        station = railway_s.result[i];

        // reverse the coordinates[1] and coordinates[0] if needed
        locations.push([station.stationname, station.latlong.coordinates[1], station.latlong.coordinates[0], station.crscode, distance]);

        $("#railway_s_ul").append('<li class="blk"><h2 class="org_name">' + station.stationname + '<a href="http://maps.google.com/maps?q='+ station.latlong.coordinates[1] +','+ station.latlong.coordinates[0] +'" class="btn btn-primary" target="_blank">view on map</a><span class="org_id">Station Code: ' + station.crscode + '</span></h2><div class="distance">Distance: ' + distance + ' miles</div></li>')
}