Coder Social home page Coder Social logo

Comments (5)

perliedman avatar perliedman commented on June 16, 2024 3

Hi there,
you mean you want to update the style for all features in the layer?

There is currently no method for doing that. You could theoretically achieve this by using getFeatureId option, and then finding all features through the private member _vectorTiles and settings their style, but that would be a hack.

This seems like a useful feature, though, so I'll keep the issue open and think about if it could be implemented.

from leaflet.vectorgrid.

abenrob avatar abenrob commented on June 16, 2024

duplicate/related to #50

from leaflet.vectorgrid.

gregallensworth avatar gregallensworth commented on June 16, 2024

I have done what you describe: override/replace the vectorTileLayerStyles for a specific layer, then call its redraw() in order to apply the new style.

// the VectorGrid setup, with initial color schemes
mylayer = L.vectorGrid.protobuf(URL_HERE, {
	rendererFactory: L.canvas.tile,
	interactive: true,
	vectorTileLayerStyles: {
		easythings: {
			fill: true, fillColor: 'blue', fillOpacity: 1,
			color: 'rgb(0, 0, 0)', opacity: 1, weight: 0.5,
		},
		bigthings: {
			fill: true, fillColor: 'red', fillOpacity: 1,
			color: 'rgb(0, 0, 0)', opacity: 1, weight: 0.5,
		},
		otherthings: {
			fill: true, fillColor: 'yellow', fillOpacity: 1,
			color: 'rgb(0, 0, 0)', opacity: 1, weight: 0.5,
		}
	},
}).addTo(MAP);
// override the styles for one of those, to use a different fill color
// then redraw
mylayer.options.vectorTileLayerStyles.bigthings.fillColor = 'purple';
mylayer.redraw();
// override the styles for one of those, to use a callback for choosing colors
// before, it was just one solid color but now it chooses based on criteria
// then redraw
mylayer.options.vectorTileLayerStyles.easythings = function (properties, zoom) {
    var style = {
        fill: true, fillColor: 'white', fillOpacity: 1,
        color: 'rgb(0, 0, 0)', opacity: 1, weight: 0.5,
    };

    switch (properties.status) {
        case 'done':
            style.fillColor = 'orange';
            break;
        case 'happening':
            style.fillColor = 'silver';
            break;
        case 'pending':
            style.fillColor = 'blue';
            break;
        default:
            style.fillColor = 'green';
            break;
    }

    return style;
};
mylayer.redraw();

An improvement I would like to see, would be an alternative to redraw() to simply reapply the styles, without re-downloading the tiles. As it stands, the tiles are re-downloaded and this makes for a relatively slow visual experience as well as network usage and server hits. I think I'll open an Issue for that, and see about doing that...

from leaflet.vectorgrid.

tapiamcclung avatar tapiamcclung commented on June 16, 2024

And should it be possible to achieve the same behavior with a L.vectorGrid.slicer layer?
I tried the same approach you suggest, the layer only flashes but its style is not updated.
Inspecting layer.options.vectorTileLayerStyles.sliced reveals that whatever style I am setting gets changed there, just not on the tiles.

from leaflet.vectorgrid.

tapiamcclung avatar tapiamcclung commented on June 16, 2024

As an update to my previous post/question, I found that if I only set a property and redraw the layer, it doesn't work:

layer.options.vectorTileLayerStyles.sliced.fillColor = 'green';
layer.redraw();

whereas setting it to be a function and return the style, does:

layer.options.vectorTileLayerStyles.sliced = () => {
  return {
     fill: true,
     fillColor: 'green',
     fillOpacity: 0.25,
     stroke: true,
     color: 'green',
     weight: 1
  }
}
layer.redraw();

I had to include fill for fillColorto work... Not sure why, though.

from leaflet.vectorgrid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.