2013-04-10 90 views
0

我面临一个让我发疯的问题...
我相信你会比我更新鲜,理解为什么第二个标记侦听器不工作。 :-(gmap听众不工作

在这段代码中,我从一个JSON格式的阿贾克斯查询得到一些项目

下面是javascript代码:

// Get all items in JSON format 
function getItems() 
{ 
    // Ajax call 
    $.getJSON("/index/test", function(data) { 
     createMenu(data); 
     fillMap(data); 
    }); 
} 

function fillMap(data){ 
    // For each 
    $.each(data, function(key, item) { 
     // Create markers 
     var latLon = new google.maps.LatLng(item.lat,item.lon); 
     marker = new google.maps.Marker({ 
      position:  latLon, 
      map:   map, 
      title:  'Index: ' + item.id, 
     }); 
     // Set marker on the map 
     marker.setMap(map); 

     // Listener 1 
     google.maps.event.addListener(marker, "click", function() { 
      map.setCenter(marker.getPosition()); 
     }); 

     // Listener 2 - 
     google.maps.event.addListener($('#resultList-'+item.id)[0], 'click', function() { 
      map.setCenter(marker.getPosition()); 
     }); 

    }); 
} 


//Create the HTML menu 
function createMenu(data){ 
    var items = []; 
    //For each items 
    $.each(data, function(key, item) { 
     var imgHtml = '<img class="shadow" src="../images/item.png" height="48" width="48" alt="photo">'; 
     // Create the HTML li tag 
     var html = '<li id="resultList-' + item.id + '" class="resultList">' + imgHtml + item.nom + ' ' + item.prenom + '<br/>' + item.adresse + '<br/>' + item.ville + ', ' + item.pays + '</li>'; 
     items.push(html); 
    }); 

    // Fill the div 
    $('<ul/>', { 
     'id': 'resultList', 
     'class': 'resultList', 
     html: items.join('') 
    }).appendTo('#divList'); 
    //End... 
} 

提前感谢 塞德里克

回答

0

如果你想添加一个非gmaps对象的事件监听器,你应该使用google.maps.event.addDomListener

所以你可以试试这个:

google.maps.event.addDomListener(document.getElementById('resultList-' + item.id), 'click', function() { 
    map.setCenter(marker.getPosition()); 
}); 
+0

非常感谢......! – 2013-04-10 23:09:27