question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recalculate the style of a GeoJSON layer based on reactive variables change

See original GitHub issue

I’m sorry that I write a question here, but for some reason, StackOverflow says there is no tag for Vue2Leaflet, and I don’t have enough reputation to create a new tag. This might actually be a bug / enhancement so hopefully it will be ok.

I have a Vue2Leaflet map with a GeoJSON layer. This layer has a style that depends on additional data from the features in the GeoJSON data, and also on an external parameter (the color depends on data, and the data to show depends on a selection of the user).

When I change the external property, the style doesn’t update reactively. Is there a way to tell the style to recalculate itself?

I tried to access the mapObject and to re-set the style property (even though it’s not strictly necessary) but I got an error saying the mapObject was undefined.

My code looks something like this:

    <v-map ...>
        <v-tilelayer ... />
        <v-geojson-layer ref="geojsonLayer" :options="geoOptions" 
            :geojson="geojson" />
    </v-map>

with the style function (simplified):

    geoOptions: {
        style: feature => {
            if (this.var1 === '') return {}
            let data = feature.properties.data
            if (data[this.var1] === undefined) return {}
            if (data[this.var1][this.var2] === undefined) return {}
            return {
                fillColor: scale(data[this.var1][this.var2]).hex()
                ...
            }
        }
    }

where scale() is a chroma.js colorscale that depends on the value of this.var1. What I would like to have is that whenver this.var1 or this.var2 change (they are reactive variables), the style of the features will get re-evaluated.

I hope I expressed my problem clearly.

Thank you very much! Omri

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
DonNicoJscommented, Aug 22, 2019

@cordlesstuba you can do something like this:

  1. create computed property that changes anytime ANY of the properties that you are interested in change
  2. add a watcher on such property
  3. add a code like this in the watcher body
updateGeoJsonStyle (newStyle) {
      this.$nextTick(() => {
        if (this.$refs.geoJson && this.$refs.geoJson.mapObject) {
          this.$refs.geoJson.mapObject.eachLayer((layer) => {
              layer.setStyle(newStyle);
          });
        }
      });
    }
2reactions
JWDobkencommented, Jul 19, 2018

I had the same issue but with mouse hovering and managed to implement the setStyle option onEachElement, like:

function onEachFeature (feature, layer) {
    layer.on('mouseover', function (e) {
        e.target.setStyle({
            fillColor: '#ff0033',
        });
    });
    layer.on('mouseout', function (e) {
        e.target.resetStyle()
    });
}

Only the resetStyle does not work yet

Read more comments on GitHub >

github_iconTop Results From Across the Web

Recalculate the style of a GeoJSON layer based on reactive ...
This layer has a style that depends on additional data from the features in the GeoJSON data, and also on an external parameter...
Read more >
GeoJSONLayer | ArcGIS Maps SDK for JavaScript 4.25
The GeoJSONLayer class is used to create a layer based on GeoJSON. GeoJSON is a format for encoding a variety of geographic data...
Read more >
Changing the style of each feature in a Leaflet GeoJSON layer
My code example changes the style of each of the feature instances within geojsonLayer based on the value of a specified property on...
Read more >
Customizing the behavior of cached fields - Apollo GraphQL
You can customize how a particular field in your Apollo Client cache is read and written. To do so, you define a field...
Read more >
Leaflet for R - Using Leaflet with Shiny - GitHub Pages
This works, but reactive inputs and expressions that affect the renderLeaflet expression will cause the entire map to be redrawn from scratch and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found