【发布时间】:2011-09-18 09:41:47
【问题描述】:
我编写了一个简短的脚本来获取一组位置对象(由 PHP 生成)并将它们绘制在 Google 地图上。
对于我的一生,我无法弄清楚为什么 addMarker() 循环会中断?
我发送给initializeMap()的数组示例:
[
{
date: '08/11/2011',
venue: 'Notes',
city: 'Newtown, NSW',
ticket: 'http://noteslive.net.au/'
}
]
还有代码:
// Accepts an array of gigs
function initializeMap(gigs) {
var markers = gigs;
// Create geocoder
var geocoder = new google.maps.Geocoder();
// Create the map
var map = new google.maps.Map(document.getElementById("gigpress-map"), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Create infowindow object
var infowindow = new google.maps.InfoWindow({
content: "holding..."
});
// Add markers to map
for (index in markers) addMarker(markers[index]);
function addMarker(data) {
alert('bang');
geocoder.geocode( { 'address': data.city}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker( {
position: results[0].geometry.location,
map: map,
title: data.venue
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
// Bind click event to each marker
google.maps.event.addListener(marker, "click", function() {
infowindow.open(map, this);
});
}
// Zoom and center the map to fit the markers
var bounds = new google.maps.LatLngBounds();
for (index in markers) {
var data = markers[index];
geocoder.geocode( { 'address': data.city}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
bounds.extend(new google.maps.LatLng(results[0].geometry.location));
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
map.fitBounds(bounds);
}
【问题讨论】:
-
哪里坏了?它是成功地进行地理编码还是没有请求去地理编码器?
-
在 jsfiddle 上托管代码并尝试在那里重现错误并使用链接更新问题
-
是的,它成功地进行了地理编码。循环未完成。请耐心等待,我是 jsfiddle 的新手,我现在将在那里主持。
-
这里是 jsfiddle 的链接...jsfiddle.net/KaSjH
标签: javascript google-maps-api-3 geocoding