Mapbox-gl-0.44.1:如何禁用单个拉伸建筑物的渲染?
Got it, let's break down why your filter isn't working and fix it right away!
The core issue here is strict type mismatch. In Mapbox's building source-layer, the id field is stored as a numeric value, not a string. But your original filter uses a quoted string "12345" for comparison—this means the check will never return true (since a number can't equal a string), so the target building still renders.
Corrected Code
map.on('load', function () { map.addLayer({ 'id': '3d-buildings', 'source': 'mapbox', 'source-layer': 'building', "filter": ["!=", "id", 12345], // Removed quotes to use numeric type 'type': 'fill-extrusion', 'paint': { 'fill-extrusion-color': '#bbb', 'fill-extrusion-height': 10, 'fill-extrusion-base': 0, 'fill-extrusion-opacity': 1 } }); });
Quick Checks to Guarantee It Works
- Verify the target building's ID is correct: Use
map.queryRenderedFeatures()in your browser console or Mapbox Studio to inspect the building's properties—confirm theidis indeed a number, not a string. - For extra compatibility with v0.44.1 (a fairly old version), you can wrap the filter in an
allclause (though it's not strictly necessary here):"filter": ["all", ["!=", "id", 12345]]
Why This Mismatch Matters
Mapbox GL JS filters enforce strict type equality. When you compare a numeric ID to a string literal, the engine sees them as entirely different values—so your != check always passes, and no buildings get excluded. Fixing the type alignment makes the filter behave as expected.
内容的提问来源于stack exchange,提问作者Stranger in the Q

