2015-06-23 7 views
6

Sto cercando di restituire i marcatori come oggetto ma quando eseguo la funzione restituisce [], ma stampandolo all'interno posso vedere i dati dell'oggetto, qualcuno può spiegare come per restituire l'oggetto batch2 per favore?restituire oggetto da una funzione javascript con query di database all'interno di

google.maps.event.addListener(mgr, 'loaded', function(){ 
      mgr.addMarkers(getMarkers(),6); //add all the markers! documentation for viewports with totals for city count, look at viewport 
      mgr.addMarkers(getMarkers2(),14); //get markers for zoomed out place, add click function to zoom in 
      //mgr.addMarkers(getMarkers(1000), 8); 
      console.log("added"); 
      mgr.refresh(); 
     }); 

function getMarkers2() { 

     var batch2 = []; 
     var clusters = new Parse.Query("cityfreqcoords"); 
     var clusterresults = new Parse.Object("cityfreqcoords"); 

     clusters.find({ 
      success: function (results) { 
       for (i = 1; i < results.length; i++) { 

        var city = (results[i]["attributes"]["city"]); 
        var count = (results[i]["attributes"]["count"]); 

        var lat = (results[i]["attributes"]["lat"]); 
        var lng = (results[i]["attributes"]["lng"]); 

        var markerLatlong = new google.maps.LatLng(lat, lng); 

        //icon = 

        //adding the marker 
        var marker2 = new google.maps.Marker({ 
         position: markerLatlong, 
         title: city, 
         clickable: true, 
         animation: google.maps.Animation.DROP 
         //icon:icon 
        }); 

        //adding the click event and info window 
        google.maps.event.addListener(marker2, 'click', function() { 
         map.setZoom(6); 
         map.setCenter(marker2.getPosition()); 
        }); 
        batch2.push(marker2); 
       } 
      } 
     }) 
     return batch2; 
    } 

risposta

0

finito per passare facendo il manager marcatore globale e passando mons nella query che ha funzionato, probabilmente non è il modo più efficace per farlo

function getMarkers2(mgr) { 

     Parse.initialize("X", "Y"); 

     var batch2 = []; 
     var clusters = new Parse.Query("cityfrequency2"); 
     var clusterresults = new Parse.Object("cityfrequency2"); 

     clusters.find({ 
      success: function (results) { 
       for (i = 0; i < (results.length); i++) { 

        var city = (results[i]["attributes"]["city"]); 

        var lat = (results[i]["attributes"]["lat"]); 
        var lng = (results[i]["attributes"]["lng"]); 

        var markerLatlong = new google.maps.LatLng(lat, lng); 

        var image = { 
         url: 'warning.png', 
         size: new google.maps.Size(50, 46), 
         // The origin 
         origin: new google.maps.Point(0, 0), 
         // The anchor 
         anchor: new google.maps.Point(25, 0) 
        }; 

        //adding the marker 
        var marker2 = new google.maps.Marker({ 
         position: markerLatlong, 
         title: city, 
         clickable: true, 
         animation: google.maps.Animation.DROP, 
         icon:image 
        }); 

        //adding the click event and info window 
        google.maps.event.addListener(marker2, 'click', function() { 
         map.setZoom(6); 
         map.setCenter(); 
        }); 
        batch2.push(marker2); 
        mgr.addMarkers(batch2,0,6); 
        mgr.refresh(); 
       } 
      } 
     }) 
    } 

    function setupMarkers() { 
     var mgrOptions = { borderPadding: 50, maxZoom: 15, trackMarkers: true }; 
     mgr = new MarkerManager(map,mgrOptions); 

     google.maps.event.addListener(mgr, 'loaded', function(){ 

      getMarkers2(mgr); 
      getMarkers(mgr); 

      console.log("added"); 
     }); 
    } 
0

Con callback in javascript, in genere non si restituiscono dati. Si passa in un'altra funzione riferimento in il gestore come un callback.

EG:

function getMarkers2(f) { 
    // do stuff 
    //when done 
    f(batch2) 
} 
4

Sembrerebbe che clusters.find è asincrona. Si restituisce batch2 prima di cluster.find succede. Ci sono una serie di modelli per lavorare con il codice asincrono in JavaScript: uno comune è usare un callback. Si avrebbe bisogno di riscrivere il codice in questo modo:

function getMarkers2(callback) { 

    var batch2 = []; 
    var clusters = new Parse.Query("cityfreqcoords"); 
    var clusterresults = new Parse.Object("cityfreqcoords"); 

    clusters.find({ 
     success: function (results) { 
      for (i = 1; i < results.length; i++) { 

       var city = (results[i]["attributes"]["city"]); 
       var count = (results[i]["attributes"]["count"]); 

       var lat = (results[i]["attributes"]["lat"]); 
       var lng = (results[i]["attributes"]["lng"]); 

       var markerLatlong = new google.maps.LatLng(lat, lng); 

       //icon = 

       //adding the marker 
       var marker2 = new google.maps.Marker({ 
        position: markerLatlong, 
        title: city, 
        clickable: true, 
        animation: google.maps.Animation.DROP 
        //icon:icon 
       }); 

       //adding the click event and info window 
       google.maps.event.addListener(marker2, 'click', function() { 
        map.setZoom(6); 
        map.setCenter(marker2.getPosition()); 
       }); 
       batch2.push(marker2); 
      } 
     } 

     callback(batch2); 
    }) 
} 

Quindi chiamare in questo modo:

getMarkers2(function(markers) { 
    mgr.addMarkers(markers, 14); 
}); 

Se siete interessati, date un'occhiata a come le promesse funzionano come si potrebbe preferire che approccio rispetto usando i callback.

+0

Hey grazie per la risposta, ho provato questo ed è ancora dando undefined –

+0

@Ali_bean sì, il codice sta facendo alcune cose strane. Suggerirei di capire come funziona async e poi tornare ad esso. – Cymen

+0

Darà un'occhiata in esso, grazie! Ho 5k markers e ho bisogno di essere in grado di utilizzare il gestore di marker –

Problemi correlati