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);
}
});
}