2013-06-28 4 views
0

J'ai besoin de créer des pop-ups qui affichent des informations provenant de différentes requêtes ajax sur une carte. Pour faire le deuxième appel j'utilise un userID que je reçois du premier appel. Les informations de la deuxième appel, il a montré bon, mais tous les pop ups ont montré le même nom, du dernier élément du premier appel, pourquoi cela se produit, quelqu'un peut-il m'aider s'il vous plaît? Merci d'avance!!!.Problème avec plusieurs rappels getJSON jquery

$.getJSON('url', function (data) { // first call 
     for (var i = 0; i < data.length; i++) {    
     var name= data[i].name; 
     var location= data[i].location; 
     var userID = data[i].userID; 
     var myIcon= data[i].icon; 
     var marker = new L.Marker(location, {title: name, icon: myIcon});// create the marker   
     $.getJSON('https://api.site.com/data/'+userID+'',(function(marker){ // second call using userID 
      return function(data2) { 
      var info1 = data2.response.tips.items[0].text; 
      var info2 = data2.response.tips.items[1].text; 
      marker.bindPopup("<div class='popup'>" + name +"</br>"+ info1 +"</br>"+ info2 + "</div>", {maxWidth: '600'}) // create the popup and add it to the marker      
       } 
     })(marker) 
     ); 
     shops.addLayer(marker); // add marker to map layer 
     } 
+0

Vous devez soit obtenir 'name' du' marker' ou ajouter 'name' aux variables de votre clôture. – Barmar

+0

désolé @Barmar mais, pouvez-vous m'expliquer plus? Merci!! – Pela

+0

Je pensais que vous comprendriez, puisque vous l'avez déjà fait pour 'marker'. J'ai posté une réponse qui le montre. – Barmar

Répondre

0

Vous devez capturer name la fermeture, comme vous le faites pour .

$("#loadingDiv").show(); 
    $.getJSON('https://api.site.com/data/'+userID+'',(function(marker, name){ // second call using userID 
     return function(data2) { 
     var info1 = data2.response.tips.items[0].text; 
     var info2 = data2.response.tips.items[1].text; 
     marker.bindPopup("<div class='popup'>" + name +"</br>"+ info1 +"</br>"+ info2 + "</div>", {maxWidth: '600'}) // create the popup and add it to the marker 
     $("#loadingDiv").hide();     
      } 
    })(marker, name) 

<div id="#loadingDiv" style="display: none">Loading...</div>