json undefined var

时间:2012-05-28 10:26:33

标签: javascript google-maps

因此,我尝试将Google地图功能与以下代码和json数据结合使用。 但我得到错误:位置未定义错误。我真的不明白为什么,因为我在我的代码中定义了。 我是JavaScript的新手,任何建议都要对此代码表示赞赏。

    var data = <%=JSON.stringify(Info, null, 2) & vbNewline%>;

  function initialize() {
        var center = new google.maps.LatLng(48.404840395764175, 2.6845264434814453);

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 3,
      center: center,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var markers = [];
    for (var i = 0; i < 100; i++) {
      var location = data[i];
      var latLng = new google.maps.LatLng(location.latitude,
          location.longitude);
      var marker = new google.maps.Marker({
        position: latLng
      });
      markers.push(marker);
    }
    var markerCluster = new MarkerClusterer(map, markers);
  }
  google.maps.event.addDomListener(window, 'load', initialize);


{ "firstname": "", "lastname": "", "location": { "latitude": "48.4048403957642", "longitude": "2.68452644348145" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.4050236871384", "longitude": "2.68512690051361" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.4048403957642", "longitude": "2.68452644348145" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.7570941168018", "longitude": "2.16670989990234" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.404922961092", "longitude": "2.70020564018949" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.8739279353421", "longitude": "2.32875823974609" } } { "firstname": "", "lastname": "", "location": { "latitude": "48.4048403957642", "longitude": "2.68452644348145" } } { "firstname": "", "lastname": "", "location": { "latitude": "41.0349860434783", "longitude": "28.9774059609177" } } 

1 个答案:

答案 0 :(得分:3)

你应该做

for (var i = 0; i < data.length; i++) {

而不是

for (var i = 0; i < 100; i++) {