Я написал короткий скрипт, чтобы взять массив объектов местоположения (сгенерированных 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);
}