I managed to create a VectorTileLayer and add it to my map (code at the end of this question).
My question is: How I can change the styling of data in a vector tile layer without it having to reload all data in the viewport? My dataset is quite big (about 1MB at zoom level 13+) and I want to avoid too much traffic.
I searched GeoNET and found a few threads about styling and changing styling, for example:
The ArcGIS Javascript 4.9 API documentation on Vector Tiles says (emphasis mine):
Styles may contain multiple options for rendering the same type of feature. In a street layer, for example, major highways might have three symbology options. The symbology can be changed on the client without having to make a request for a new tile from the server.
However I am unable to find any working examples where styles are changed live and without additional requests. The ArcGIS Vector Style Editor has this issue as well.
What I tried:
- Overwriting my layer's "style" property, no visible change
- Overwriting my layer's "currentStyleInfo" property, no visible change
- Poke around in some other internal properties... no visible change
- Use layer.loadStyle(JSONObject) - applies the new styling, but reloads all data
- Provide a few styles in the initial styles "layers" property - only the last style provided is applied, how do I change between those?
Example code:
- creates a VectorTileLayer
- styling is circles that have a color ramp fill-color based on an attribute
<SPAN class="keyword token">var</SPAN> vtLayer <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">VectorTileLayer</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN>
minScale<SPAN class="punctuation token">:</SPAN> <SPAN class="number token">140000</SPAN><SPAN class="punctuation token">,</SPAN>
style<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"version"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">8</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"sprite"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fexample.com%2FVectorTileServer%2Fresources%2Fsprites%2Fsprite" target="_blank">https://example.com/VectorTileServer/resources/sprites/sprite</A><SPAN>"</SPAN></SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"glyphs"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fexample.com%2FVectorTileServer%2Fresources%2Ffonts%2F" target="_blank">https://example.com/VectorTileServer/resources/fonts/</A><SPAN>{fontstack}/{range}.pbf"</SPAN></SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"sources"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"esri"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"type"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"vector"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"url"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token"><SPAN>"</SPAN><A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fexample.com%2FVectorTileServer" target="_blank">https://example.com/VectorTileServer</A><SPAN>"</SPAN></SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"layers"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"id"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"my_attrib/min"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"type"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"circle"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"source"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"esri"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"source-layer"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"my_attrib"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"filter"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN>
<SPAN class="string token">"=="</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"_symbol"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="number token">2</SPAN>
<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"layout"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"icon-image"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"my_attrib/min"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"icon-allow-overlap"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">true</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"paint"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"circle-radius"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"circle-stroke-width"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"circle-stroke-color"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"#FFFFFF"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"circle-color"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"property"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"my_attribute"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"default"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"#000000"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"stops"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"#FF0000"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"#FFFF00"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"#00FF00"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"#00FFFF"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"#0000FF"</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>