2017-07-13 2 views
0

Ich versuche, für jedes Polygon auf einer Karte eine Füllfarbe zuzuweisen, ich folge leaflet example for choropleth, das Beispiel erzeugt die Farben basierend auf einer Eigenschaft auf der Geojson-Datei, z. mit jedem "properties":{"name": oder "id":"density":94.65Wie generiert man einen Füllfarbenbereich für Polygone auf einer Karte?

// get color depending on population density value 
function getColor(d) { 
    return d > 1000 ? '#800026' : 
      d > 500 ? '#BD0026' : 
      d > 200 ? '#E31A1C' : 
      d > 100 ? '#FC4E2A' : 
      d > 50 ? '#FD8D3C' : 
      d > 20 ? '#FEB24C' : 
      d > 10 ? '#FED976' : 
         '#FFEDA0'; 
} 

function style(feature) { 
    return { 
     weight: 2, 
     opacity: 1, 
     color: 'white', 
     dashArray: '3', 
     fillOpacity: 0.7, 
     fillColor: getColor(feature.properties.density) 
    }; 
} 

ich, dass Anstand nicht verwenden, ist zu tun, was ich möchte die Füllfarben erzeugen wahrscheinlich

Das ist, was ich versuche:

function style(feature) { 
    jQuery("svg path").each(function(){ 
     myColour = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6) 
    }); 
    return { 
     weight: 2, 
     opacity: 1, 
     color: 'white', 
     dashArray: '3', 
     fillOpacity: 0.7, 
     fillColor: myColour 
    }; 
} 

var geojson = L.geoJson(statesData, { 
    style: style, 
}).addTo(map); 

Uncaught ReferenceError: myColour is not defined

Antwort

0

Gelöst es:

function style(feature) { 
    return { 
     weight: 2, 
     opacity: 1, 
     color: 'white', 
     dashArray: '3', 
     fillOpacity: 0.7, 
     fillColor: '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6) 
    }; 
} 
Verwandte Themen