2amigos / yii2-leaflet-extension

Yii 2 Extension library to display interactive maps with Leaflet .
http://yiiwheels.com
Other
30 stars 26 forks source link

CHOROPLETH MAP: how to add javascript functions through GeoJson clientOptions? #40

Closed proyectotestbuk closed 6 years ago

proyectotestbuk commented 6 years ago

Hi! Thanks for this fantastic extension.

I'm really lost with Javascript and definetely don't know how to add functions described in: http://leafletjs.com/examples/choropleth/ in my \dosamigos\leaflet\layers\GeoJson

I can set color using $capaProvincias = new \dosamigos\leaflet\layers\GeoJson([ 'data' => $geojson, 'clientOptions' => [ 'style' => [ 'color' => 'green' ], ], ]);

but I need different colors for each feature. Something like this:

$capaProvincias = new \dosamigos\leaflet\layers\GeoJson([ 'data' => $geojson, 'clientOptions' => [ 'style' => 'js:function(feature) { switch (feature.properties.Cod_Prov) { case "01": return {color => "#ff0000"}; case "02": return {color => "#ffff00"}; case "03": return {color => "#ff00ff"}; } }', ], ]);

Lots of hours lost working in this thing due to my incompetency! :-(

Thanks in advance!

proyectotestbuk commented 6 years ago

Finally resolved with: 'clientOptions' => [ 'style' => new JsExpression ('function(feature) { switch (feature.properties.Cod_Prov) { case 6: return {color: "red"}; break; default: return {color: "blue"}; } }') ]

As in https://stackoverflow.com/a/46980728/2437857

tonydspaniard commented 6 years ago

@proyectotestbuk very happy you found the issue and really sorry for not being able to be more responsive.