0

I have a map that relates to a table. When the user selects a table row, I would like to highlight the corresponding map feature. This is the only way I found to do it:

const siteGeoJSON = SitesToGeoJSON(sites, activeId);
const newSource = new carto.source.GeoJSON(siteGeoJSON);
siteLayer.update(newSource, siteViz);

What I'm doing here is regenerating the entire GeoJSON object to set a single property indicating whether each feature is active or not, then digesting that into Carto anew. Features with the active property have a different symbol.

This probably isn't a very efficient way to do this, though. Is there any way to change feature properties without creating a new source?

  • This doesn't directly answer this question, but it solved the problem. Instead of changing the property, I found a way to change the presentation: I hold on to siteViz then iterate through the features contained in siteViz.variables.visibleFeatures.value to find the site I want to change, and enlarge it by using f.width.blendTo(16, 200) and later f.width.reset(). – ed94133 Sep 17 '19 at 18:48
  • If it works, I would say it is a valid answer. So I would suggest posting your answer in more detail and more code snippets (or link to working example). – ramiroaznar Sep 18 '19 at 7:51
0

I solved this by holding onto siteViz as an upper-scope Javascript variable, initializing it with this: @visibleFeatures: viewportFeatures($id)

When the user hovers on a table row, I use State in React to set an activeId, and a corresponding side-effect function (useEffect) updates the map:

    siteViz.variables.visibleFeatures.value.forEach(f => {
      if (f.properties.id === activeId) {
        f.width.blendTo(16, 200);
      } else {
        f.width.reset();
      }
    });
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.