0

因此,我尝试将 Google Maps 功能与以下代码和 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" } } 
4

1 回答 1

3

你可能应该做

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

代替

for (var i = 0; i < 100; i++) {
于 2012-05-28T10:29:32.583 回答