Domanda

Sto creando un progetto utilizzando la libreria Polymaps JS.Devo tracciare circa 200.000 punti.Ci vuole un po 'per caricare i punti nel browser e quindi la navigazione è molto lenta.

Ho letto la documentazione e non esiste alcuna opzione per filtrare un GeoJson prima di aggiungere i suoi dati alla pagina.

Qualcuno può suggerire un modo migliore di questo:

var po = org.polymaps;
var map = po.map()
.container(document.body.appendChild(po.svg("svg")))
.center({lat: 45.468318, lon: 9.1709})
.zoom(13)
.add(po.interact());

//Skinning the map
map.add(po.image()
.url(po.url("http://{S}tile.cloudmade.com"
+ "/1a1b06b230af4efdbb989ea99e9841af" // http://cloudmade.com/register
+ "/998/256/{Z}/{X}/{Y}.png")
.hosts(["a.", "b.", "c.", ""])));

//Importing the geoJSON
map.add(po.geoJson()
.url("test_4sq.json")
.id("streets")
.on("load", loadAreas)
.tile(false));

map.add(po.compass()
.pan("none"));

// This function loads all the data and then do the filtering (very sluggish method) 
function loadAreas(obj) {
for (var i = 0; i < obj.features.length; i++) {
    var f = obj.features[i];
    var e = obj.features[i].element;
    var p = obj.features[i].data.properties;
    e.setAttribute('r', 4);
    e.setAttribute('id', f.data.id);
    e.setAttribute('title', p.venueName);
    //console.log(e);

    // Displaying the data in August (month propriety = '8')
    if (p.month != "08")
         console.log(e); 
    else 
        e.setAttribute('display', 'none');
} 
} 
È stato utile?

Soluzione 2

Ho capito come rendere lo script più veloce. Prima di tutto avviare l'app tramite localhost o server rende tutto più veloce. Apro sempre l'app tramite file (file: ///pathTo_file/index.html)!È sbagliato.È meglio invece utilizzare un server (www.pathTo_file.com/ o localhost: //pathTo_file/index.html) In secondo luogo, ho provato a minimizzare il json importato.Ho lasciato molti spazi e interruzioni di riga per una migliore leggibilità, ma e 'stato abbastanza pesante da caricare quindi ho rimosso tutti questi caratteri inutili. Terzo, carico un file solo se un utente usa il daypicker.In questo modo l'app carica prima tutte le tessere e una seconda volta i dati scelti dall'utente.

Ecco un esempio del codice se qualcuno è interessato.

$(document).ready(function() {



// DAY AND MONTH CORRECTION RELATED TO FILENAME
function addZero(num){
    console.log("Function launched: addZero")
    parseInt(num);
    if(num>=1 && num<=9){
            num="0"+num;
    }   
    return num;
} 

$("#datepicker").datepicker({
    dateFormat: 'yy/mm/dd',
    inline: true,
    minDate: new Date(2011, 8 - 1, 20),
    maxDate:new Date(2011, 12 - 1, 31),
    altField: '#datepicker_value',
      onSelect: function(){
        var selDay = $("#datepicker").datepicker('getDate').getDate();                 
        var selMonth = $("#datepicker").datepicker('getDate').getMonth() + 1;             
        var selYear = $("#datepicker").datepicker('getDate').getFullYear();


//PLOTTING THE MAP WITH THE USER'S SELECTION - DATEPICKER PARAMETERS -
plotMap(addZero(selDay), addZero(selMonth));
    }
});
//INITIALISATING THE DATEPICKER 
$("#datepicker").datepicker('setDate', new Date());


    // JSON DATA IMPORT
    var po = org.polymaps;
    var map = po.map()
            .container(document.body.appendChild(po.svg("svg")))
            .center({lat: 45.468318, lon: 9.1709})
            .zoom(13)
            .add(po.interact());

    map.add(po.image()
            .url(po.url("http://{S}tile.cloudmade.com"
            + "/1a1b06b230af4efdbb989ea99e9841af" // http://cloudmade.com/register
            + "/998/256/{Z}/{X}/{Y}.png")
            .hosts(["a.", "b.", "c.", ""])));

    function plotMap(day, month){
        var jsonUrl = "json/4sq_"+day+"_"+month+"_min.json";
        map.add(po.geoJson()
            .url(jsonUrl)
            .on("load", loadSingleEvents));

            console.log("Ho caricato il file:" + jsonUrl);
    };


    map.add(po.compass()
            .pan("none"));


//LOADING THE DATA
    function loadSingleEvents(obj) {
        console.log("Function launched: loadSingleEvents")
        singleEvents=true;

        $.each (obj.features, function (i, feat){
            var point = feat.element;
            var propriety = feat.data.properties;
            point.setAttribute('r', 3.5);
            point.setAttribute('id', feat.data.id);
            point.setAttribute('data-venueName', propriety.venueName);
            point.setAttribute('data-hour', propriety.hour);        
        }); 
        console.log("Numero di Elementi visualizzati: (dovrebbe essere sui 3500) "+ obj.features.length);           

    }



});

Altri suggerimenti

L'utilizzo di un server di affiancamento geoJSON sembra l'unico modo per utilizzare tutti questi elementi.Dai un'occhiata a TileStache.Potrebbe essere utile anche questo: http://goalfinch.com/blog/2011/03/building-interactive-maps-with-polymaps-tilestache-and-mongodb-part-2/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top