2

I am trying to add a legend in Leaflet based on a GeoJSON layer like this:

enter image description here

This is my code:

L.geoJson(Viales_Transitables).addTo(map);
var Viales_TransitablesStyle = {                                                          
    'weight': 1.3, // grosor de línea                                                     
    'color': "#ffffff",// color de linea                                                  
    'stroke':0.2,                                                                         
    'opacity':10,                                                                         
};                                                                                        
L.geoJson(Viales_Transitables, {                                                          
    style: Viales_TransitablesStyle                                                       
}).addTo(map);

                                        
function getColor(d) {
    return d === 1 ? '#fa0000' :                                                          
    d === 2 ? '#fa9b00' :                                                                 
    d === 3 ? '#f6fa00' :                                                                 
    d === 4 ? '#44fa00' :                                                                 
    '#FFEDA0';                                                                            
}                                                                                         
                                                                                          
function style(feature) {                                                                 
    return {                                                                              
      color: getColor(feature.properties.Ancho_Tran),                                           weight: 2,                                    
      opacity: 1,                                                                         
      fillOpacity: 0.7                                                                    
    };                                                                                    
  }                                                                                       
                                                                                          
L.geoJson(Viales_Transitables, {                                                          
    style: style                                                                          
}).addTo(map);

I would like to add this legend in Leaflet but I don't know how.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.