Subversion Repositories eFlore/Applications.coel

Rev

Rev 1834 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

var map = null,
optionsCoucheOSM = {
        attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors,'
        + ' <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>',
        maxZoom: 18
},
optionsCoucheGoogle = {
        attribution: 'Map data &copy;'+new Date().getFullYear()+' <a href="http://maps.google.com">Google</a>',
        maxZoom: 18
},
coucheOSM = new L.TileLayer("http://osm.tela-botanica.org/tuiles/osmfr/{z}/{x}/{y}.png",
                optionsCoucheOSM),
coucheSatellite = new L.TileLayer("http://mt1.google.com/vt/lyrs=y@218131653&hl=fr&src=app&x={x}&y={y}&z={z}",
        optionsCoucheGoogle),
optionsCarte = {
        center : new L.LatLng(46, 2),
        zoom : 6,
        layers : [coucheOSM]
};

var xmlHttpRequest = null;
        nombreCollections = 0;
        collections = new Array();
        structures = new Array();
        if(clustering) {
                coucheStructures = new L.MarkerClusterGroup({
                        disableClusteringAtZoom : 10
                });
        } else {
                coucheStructures = new L.FeatureGroup();
        }
        infoBulle = null;
        chargementEnCours = false;


$(document).ready(function() {
        dimensionnerCarte();
        initialiserCarte();
        initialiserPanneauControle();

        $.ajax({
                dataType: "json",
                url: urlWebService + "CoelRecherche/Nombre/*/*/*/*/*/*/*/" + departement + "/*/",
                data: { formatRetour: "text/plain", pays: pays},
                async: false
        }).complete(function(msg) {
                if (! estStatutRequeteOK(msg.status)) {
                        alert(msg.responseText);
                        return;
                }
                nombreCollections = parseInt(msg.responseText);
        });
        
        chargerStructures();
});

$(window).resize(function() {
        dimensionnerCarte();
});

function dimensionnerCarte() {
        $("#map").width($(window).width());
        $("#map").height($(window).height());
}

function initialiserCarte() {
        map = L.map('map', optionsCarte);
        coucheOSM.addTo(map);
        coucheStructures.addTo(map);
        map.on('zoomend', function() {
                // controle sur le niveau de zoom uniquement a la fin du placement des structures sur la carte 
                if (chargementEnCours) {
                        chargementEnCours = false;
                        verifierZoom();
                }
        });
}

function initialiserPanneauControle() {
        var baseMaps = {
                "Plan" : coucheOSM,
                "Satellite" : coucheSatellite
        };
        var overlayMaps = {
                "Structures" : coucheStructures 
        };
        L.control.layers(baseMaps, overlayMaps).addTo(map);
}


function recupererValeurNombreCollections() {
}

function chargerStructures() {
        if (requeteEnCours()) {
                window.setTimeout('chargerStructures()', 400);
                return;
        }

        chargementEnCours = true;
        $.ajax({
                dataType: "json",
                url: urlWebService + "CoelRecherche/ParDefaut/*/*/*/*/*/*/*/" + departement + "/*/",
                data: { limit: nombreCollections, pays: pays},
                async: true
        }).complete(function(msg) {
                if (!estStatutRequeteOK(msg.status)) {
                        alert(msg.responseText);
                        return;
                }
                collections = eval("(function(){return " + msg.responseText + ";})()");
                ordonnerCollectionsParStructures();
                chargerLocalisations();
        });
}

function requeteEnCours() {
        return (xmlHttpRequest != null && xmlHttpRequest.readyState != 4);
}

function estStatutRequeteOK(x_status) {
        return (x_status == 200 || x_status == 304 || x_status == 0);
}

function ordonnerCollectionsParStructures() {
        for (var index = 0; index < collections.length; index ++) {
                var indexStructure = 0;
                while (indexStructure < structures.length && structures[indexStructure].id != collections[index].cs_id_structure) {
                        indexStructure ++;
                }
                if (indexStructure == structures.length) {
                        var structure = {
                                "id"    : collections[index].cs_id_structure,
                                "nom"   : collections[index].cs_nom,
                                "ville" : collections[index].cs_ville,
                                "code_postal" : collections[index].cs_code_postal,
                                "longitude"   : collections[index].cs_longitude,
                                "latitude"    : collections[index].cs_latitude,
                                "collections" : new Array()
                        };
                        structures.push(structure);
                }
                var collection = {
                        "id"  : collections[index].cc_id_collection,
                        "nom" : collections[index].cc_nom
                };
                structures[indexStructure].collections.push(collection);
        }
}

function chargerLocalisations() {
        var nombreStructuresAffichees = 0;
        for (var index = 0; index < structures.length; index ++) {
                if ((structures[index].longitude != null && structures[index].longitude != "")
                        && (structures[index].latitude != null && structures[index].latitude != "")) {
                        var existeMarqueur = rechercherExistenceMarqueur(structures[index].longitude, structures[index].latitude);
                        if (existeMarqueur == null) {
                                creerMarqueur(structures[index]);
                                nombreStructuresAffichees ++;
                        } else {
                                ajouterStructureAMarqueur(existeMarqueur, structures[index]);
                        }
                }
        }
        if (nombreStructuresAffichees > 0) {
                map.fitBounds(coucheStructures.getBounds());
        }
}

function rechercherExistenceMarqueur(longitude, latitude) {
        var existeMarqueur = null;
        coucheStructures.eachLayer(function(layer) {
                if (layer.getLatLng().lat == latitude && layer.getLatLng().lng == longitude) {
                        existeMarqueur = layer;
                }
        });
        return existeMarqueur;
}

function creerMarqueur(structure) {
        var latlng = new L.LatLng(structure.latitude, structure.longitude);
        var marqueur = new L.Marker(latlng, {
                structures : [structure],
                structuresNom : structure.nom
        });
        marqueur.on('click', surClickMarqueur);
        marqueur.on('mouseover', construireToolTipMarqueur);
        coucheStructures.addLayer(marqueur);
}

function construireToolTipMarqueur(event) {
        // changer la propriété title du marqueur ne fonctionne pas
        // en dehors du constructeur (mais cette méthode est-elle pérenne ?)
        // le clustering n'affichant pas tous les marqueurs, on doit remplir le tooltip
        event.target._icon.title = event.target.options.structuresNom;
}

function ajouterStructureAMarqueur(marqueur, structure) {
        marqueur.options.structures.push(structure);
        marqueur.options.structuresNom += "\n"+structure.nom;
}

function surClickMarqueur(event) {
        var latlng = event.target.getLatLng();
        var structures = event.target.options.structures;
        afficherCollections(structures, latlng);
}

function afficherCollections(structures, latlng) {
        masquerInfoBulle();
        infoBulle = new L.Popup({maxWidth : 0.25*$(window).width(), maxHeight : 0.35*$(window).height()});
        infoBulle.setLatLng(latlng);
        infoBulle.openOn(map);
        remplirContenuPopup(structures);
        $("a").css("color", "#598000");
        map.setView(latlng, map.getZoom());
}

function masquerInfoBulle() {
        if (infoBulle != null && map.hasLayer(infoBulle)) {
                map.removeLayer(infoBulle);
        }
        infoBulle = null;
}

function remplirContenuPopup(structures) {
        $("#structure").empty();
        var entetePopup = {
                "ville" : structures[0].ville,
                "code_postal" : structures[0].code_postal
        }
        $("#tpl-structure-entete").tmpl(entetePopup).appendTo($("#structure"));
        
        $.each(structures, function(index, structure) {
                var structureAjout = {
                        "id"    : structure.id,
                        "nom"   : structure.nom,
                        "ville" : structure.ville,
                        "code_postal" : structure.code_postal,
                        "collections" : structure.collections
                };
                $("#tpl-structure").tmpl(structureAjout).appendTo($("#structure"));
        });
        
        infoBulle.setContent($("#structure").html());
}

function verifierZoom() {
        if(map.getZoom() > 13) {
                map.setZoom(13);
        }
}