2016-07-23 140 views
0

我想顯示一個代理機構的列表,例如電話,電子郵件等......以及每個帶地址的小谷歌地圖。該地址是動態的,並通過樹枝中的循環渲染。顯示多個谷歌地圖列表

問題是隻有第一個地圖呈現。

function initMap() { 

var x = document.querySelectorAll('div [id^=「map-」]');

for(var i = 1; i < x.length; ++i) 
{ 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(53.3496, -6.3263); 
    var mapOptions = 
    { 
     zoom: 16, 
     center: latlng 
    } 

    var stringAddress = ''; 
    var stringAddress = $("#address-"+i).text(); 

    map = new google.maps.Map(document.getElementById('map-'+ i), mapOptions); 

    codeAddress(stringAddress);//call the function 
} 

}

和我的代碼功能:

function codeAddress(address) { 

geocoder.geocode({address:address}, function(results, status) 
{ 
    if (status == google.maps.GeocoderStatus.OK) { 

     map.setCenter(results[0].geometry.location);//center the map over the result 
     //place a marker at the location 
     var marker = new google.maps.Marker(
     { 
      map: map, 
      position: results[0].geometry.location 
     }); 
    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 
}); 

}

回答

1

我找到解決辦法的循環是錯誤的。的

代替

for(var i = 1; i < x.length; ++i) 

for(var i = 1; i <= x.length; ++i)