Hi, I have a map where I create country polygons in a geofield (with geojson format).
When I map this on a leaflet view, the polygons are always blue with opacy. I added a style field (color) to the country content type and tried to use this in the "Location differentiator" settings. Unfortunatelly, the styles I define are ignored.
So can I add a color or class to the different polygons??

Comments

vistree created an issue. See original summary.

RdeBoer’s picture

You can't. Not for polygons, via the UI.

vistree’s picture

Thanx RdeBoer - but can I do it by code? Can I use a leaflet-php-function to alter the settings or do I need to use jquery?

waqasbcs’s picture

Just a beginner here. I managed to changed the polygon style by changing the code in leaflet_markercluster.drupal.js file. I think it would not be recommended to change the code directly, but since I do not have experience with drupal code so I did it this way. On line number 229 in the file I added the following code (from setStyle statement). If you're not using markercluster module, i think you can add the code to leaflet.drupal.js file inside folder for leaflet module, this file has the same function(leaflet_create_feature) with same case statement.

 case 'multipolygon':
            lFeature = Drupal.leaflet.create_multipoly(feature, lMap);
			                lFeature.setStyle({fillColor: '#FFF'});
					lFeature.setStyle({color: '#FFF'});
					lFeature.setStyle({fillOpacity: '0'}); 	
					lFeature.setStyle({weight: '2'}); 	
					//lFeature.clickable=false;//({clickable: false}); 
					console.log(feature);
            break; 
Joel MMCC’s picture

Title: Color or class for goejson polygons? » Color or class for GeoJSON polygons?

Fixed typo in Issue Title so people can find this Issue in a search for “GeoJSON.”

Also, I would very much like the ability to do this. In fact, the whole way Differentiators work should probably be improved: rather than (or in addition to, for backwards compatibility) manually selecting markers, instead select or type in a CSS Class, then you can make the Class do whatever you want in your CSS.

sano’s picture

@waqasbcs what you did can be done with CSS, because SVG can be styled.

By the way, with OpenLayers this (changing geo objects styles with view-generated classes) is possible - have done it in Drupal 6. Unfortunately I was unable to make OL work in D7 - no idea why - which is the reason I use Leaflet now. But that is a different story.

gsquirrel’s picture

You can also do this using the main leaflet module view display - there are options for vector display that can deal with tokens from views fields, so i have used a text field to populate the "color" option for filling polygons.

bcobin’s picture

@gsquirrel:

Thank you for this - great tip! For some reason, I can't get the token to output the value (it insists on outputting the key for some reason) but insofar as it's an admin-only thing, I'm fine with entering the color in hex.

Very cool... :)

gsquirrel’s picture

Great! I actually used plain text field with hex code rather than a list one, so key wasn't an issue, but I think if you want to use the key it should be possible. It should be a display option on the view field.