2017-01-29 5 views
2

Bonjour, J'ai deux fonctions getGeofences() et getTraces()Comment puis-je identifier les couches qui contiennent des polygones

getGeofences() va ajouter à la carte les polygones et getTraces() va ajouter à la carte les marqueurs et les polylignes.

contiennent tous les deux

FGgpx.clearLayers(); 

et efface ainsi les couches (ainsi que les polygones et les marqueurs)

Je voudrais savoir s'il y a un moyen d'effacer seulement les polygones ou les marqueurs avec les polylignes. Par exemple, lorsque j'appelle getGeaofences(), il efface uniquement les couches avec des polygones. Lorsque j'appelle getTraces(), il efface uniquement les marqueurs et les polylignes.

J'ai regardé la fonction eachLayers mais sans succès!

Et en passant, comment puis-je obtenir l'ID de la couche, je viens de créer! Je regarde getLayerid et getLayers() mais je ne peux apparemment pas l'utiliser correctement. L'aide sera très utile, aussi bien!

Voici un code pour vous montrer comment je construis ma carte. (J'espère fournir tout ce que vous devez aider)

var openstreet = osm = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',maxZoom: 18}); 
//var map = L.map('mapId',{drawControl: true}) 
var map = L.map('mapId') 
    .addLayer(openstreet) 
    .setView([51.505, -0.09], 8); // London 
var FGgpx = new L.FeatureGroup(); 
map.addLayer(FGgpx); 

Création des polygones

var options = { 
     position: 'topleft', 
     draw: { 
      polyline: { 
       shapeOptions: { 
        color: '#e0222c', 
        weight: 5 
       } 
      }, 
      polygon: { 
       allowIntersection: false, // Restricts shapes to simple polygons 
       drawError: { 
        color: '#61dd28', // Color the shape will turn when intersects 
        message: '<strong>Oh snap!<strong> you can\'t draw that!' // Message that will show when intersect 
       }, 
       shapeOptions: { 
        color: '#c74ed8', 
        weight: 2//, 
       } 
      }, 
      rectangle: false, // Turns off this drawing tool 
      marker: false, 
      circle: false 
      /* 
      circle: { 
       drawError: { 
        color: '#61dd28', // Color the shape will turn when intersects 
        message: '<strong>Oh snap!<strong> you can\'t draw that!' // Message that will show when intersect 
       }, 
       shapeOptions: { 
        color: '#4aaad1', 
        weight: 2//, 
        //clickable: true 
       } 
      } 
      */ 
     }, 
     //edit: false 
     edit : { 
      featureGroup: FGgpx, //REQUIRED!! 
      remove: false, 
     } 

    }; 


var drawControl = new L.Control.Draw(options); 

// https://www.npmjs.com/package/leaflet-draw 
map.addControl(drawControl); 


map.on(L.Draw.Event.CREATED, function (e) { 
    var type = e.layerType, 
    layer = e.layer; 


    var shape = layer.toGeoJSON() 
    var shape_for_db = JSON.stringify(shape); 

    // Save into mySQL 
    saveGeofences(1,shape_for_db); 


}); 

getGeofences()

function getGeofences(devise_id, clearLayer, fitBounds){ 


    $.ajax({ 
    type: "POST", 
    url: "maps/sql/getGeofences.php", 
    //data: {data:data}, 
    data: {devise_id:devise_id}, 
    success: result, 
    error: error, 
    dataType: "json" 
    }); 

    function error(data) 
    { 
    console.log("Error getGeofences"); 
    console.log(data); 

    } 

    function result(data){ 

    if(clearLayer == true){ 
     FGgpx.clearLayers(); 
    } 

    console.log("getGeofenses"); 


    for (var i = data.features.length - 1; i >= 0; i--) { 

     var getgeo_geojson = L.geoJson(data.features[i],{onEachFeature: onEachFeature}); 

    } 

    if(fitBounds == true){ 
     // fit all geofences 
    } 

    } 
} 

getTraces()

function getTrace(hours){ 


    $.ajax({ 
    type: "POST", 
    url: "maps/sql/getTraces.php", 
    data:{h:hours}, // Send parameter to get.php 
    success: result, 
    error: error, 
    dataType: "json" 
    }); 

    function error(data) 
    { 
    alert("Error"); 
    console.log("Error"); 
    console.log(data); 
    } 

    function result(data){ 
    console.log("getTrace"); 
    nb_trace = 0; // stok le nombre de traces 

    /* 
    * Clear maker but does not clear the polyline 
    */ 


    FGgpx.clearLayers(); // this clear Polygons and I do not want 


    // Pass data to geoJson and do somethin on each position 
    //var mon_geojson = L.geoJson(data,{onEachFeature: onEachFeature}); 
    var mon_geojson = L.geoJson(data,{onEachFeature: onEachFeature}); 
    mon_geojson.addTo(map); // merge to L.featureGroup() 

    /* 
    * Add Plyline 
    */ 

    // cakk connectTheDots to connext each point a polyline 
    pathCoords = connectTheDots(mon_geojson); 
    var polyline = new L.polyline(pathCoords, configPath); 
    polyline.addTo(map); 

    getGeofences(1,false,false); 


    // Fit the map to all marker 
    //map.fitBounds(FGgpx.getBounds()); 

    console.log("ivalidateSize"); 
    map.invalidateSize(); 

    if(nb_trace == 0) 
    { 
     showPopup("No trace for this period",1800); 
    } 

    } 

}; 

Répondre

0

Vous devez créer 2 groupes de fonctions distincts.

var geofences = new L.FeatureGroup(); 
var traces = new L.FeatureGroup(); 
map.addLayer(geofences); 
map.addLayer(traces); 

geofences.clearLayers(); 
traces.clearLayers(); 
+0

Excellent! Merci beaucoup . Ça marche – martin10