You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mapbox-gl-0.44.1:如何禁用单个拉伸建筑物的渲染?

Fixing the Filter to Disable a Single Building in Mapbox GL JS 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 the id is indeed a number, not a string.
  • For extra compatibility with v0.44.1 (a fairly old version), you can wrap the filter in an all clause (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:04:30