Проблема с отображением массива маркеров в Google Maps v3 - PullRequest
0 голосов
/ 18 сентября 2011

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

Ответы [ 2 ]

1 голос
/ 18 сентября 2011

проблема была в том, как вы добавляли слушателя на маркер.Я думаю, что я исправил, и я надеюсь, что вы можете увидеть свой маркер .. Вот обновление для jsfiddle, который вы опубликовали:

http://jsfiddle.net/anilkamath87/KaSjH/1/

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
            });
         google.maps.event.addListener(marker, "click", function() {  // Bind click event to each marker should be here. else marker reference not found.
            infowindow.open(map, this);
    });
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });



}
0 голосов
/ 20 сентября 2011

Для полного рабочего скрипта перейдите к JSFiddle ... Этот код поможет вам построить массив маркеров и настроить границы просмотра карты.

http://jsfiddle.net/KaSjH/6/

...