【问题标题】:mapbox change highlight 3d buildingmapbox 更改突出显示 3d 建筑
【发布时间】:2020-09-14 12:05:44
【问题描述】:

我认为没有解决方案,但也许我错了,或者将来有可能。

我正在尝试在给出确切地址或 GPS 坐标的情况下突出显示 mapbox(3d 或 2d)中的建筑物。

甚至可以在 mapbox 中唯一标识建筑物吗?

我在他们的documentation 中找不到任何东西,而且破解它似乎很困难,因为所有东西都画在一个画布上。

【问题讨论】:

    标签: mapbox mapbox-gl-js


    【解决方案1】:

    是的,这是可行的,即使是建筑物的一部分。在 Mapbox 中表示为建筑物的任何东西都基于一个或多个特征。为此,您需要查询特征,然后决定使用什么属性来过滤某些特征以区分行为。

    检查一下,这是how to change color and pointer on mouse over 上的一个小提琴,我在纽约市的所有建筑物中添加了一个过滤器,仅用于一个特征 ID,它将变为红色,改变其状态('hover ; true')和鼠标指针变为map.getCanvasContainer().style.cursor = 'pointer'

    这是相关代码:

        let mapConfig = {
          NYC: {
            origin: [-74.044514, 40.689259, 39],
            center: [-74.0137, 40.70346, 0],
            zoom: 16.2,
            pitch: 60,
            bearing: 35
          }
        }
    
        mapboxgl.accessToken = 'PUT HERE YOUR TOKEN';
        let point = mapConfig.NYC;
        var map = new mapboxgl.Map({
          style: 'mapbox://styles/mapbox/streets-v11',
          center: point.center,
          zoom: point.zoom,
          pitch: point.pitch,
          bearing: point.bearing,
          container: 'map',
          antialias: true,
          hash: true
        });
    
        map.on('style.load', function() {
    
          if (map.getSource('composite')) {
            map.addLayer({
              'id': '3d-buildings',
              'source': 'composite',
              'source-layer': 'building',
              'type': 'fill-extrusion',
              'minzoom': 14,
              'paint': {
                'fill-extrusion-color': [
                  'case',
                  ['boolean', ['feature-state', 'hover'], false],
                  '#ff0000',
                  '#ddd'
                ],
                'fill-extrusion-height': ["number", ["get", "height"], 5],
                'fill-extrusion-base': ["number", ["get", "min_height"], 0],
                'fill-extrusion-opacity': 1
              }
            }, 'road-label');
          }
    
          let fHover;
    
          map.on('click', function(e) {
            var features = map.queryRenderedFeatures(e.point, {
              layers: ['3d-buildings']
            });
            console.log(features[0].id);
          })
    
          map.on('mousemove', function(e) {
            var features = map.queryRenderedFeatures(e.point, {
              layers: ['3d-buildings']
            });
            //we will change pointer and color for 42455719
            if (features[0] && features[0].id == 42455719) {
              mouseover(features[0]);
            } else {
              mouseout();
            }
    
          });
    
          map.on('mouseout', function(e) {
            mouseout();
          });
    
          function mouseout() {
            if (!fHover) return;
            map.getCanvasContainer().style.cursor = 'default';
            map.setFeatureState({
              source: fHover.source,
              sourceLayer: fHover.sourceLayer,
              id: fHover.id
            }, {
              hover: false
            });
    
          }
    
          function mouseover(feature) {
            fHover = feature;
            map.getCanvasContainer().style.cursor = 'pointer';
    
            map.setFeatureState({
              source: fHover.source,
              sourceLayer: fHover.sourceLayer,
              id: fHover.id
            }, {
              hover: true
            });
          }
    
        });
    

    【讨论】:

    • 不错!您知道我们如何根据地址或 gps 位置获取功能吗?内部功能我能想到的唯一属性是 _x 和 _y 但对于旁边的建筑物来说,它们似乎是相同的。
    • 对于地址/坐标,您可能需要直接使用 Mapbox Geocoder 和/或 Mapbox Geocoding API,这取决于您在 UI 方面的需要。这将为您提供一个可用于突出显示的功能,如图所示
    • 感谢您的帮助。地理编码 API 会生成一个特征列表,但没有一个与鼠标点击时检索到的 id 匹配。我猜 Geocoding API 不会返回 3D 地图中的特征,而是返回法线地图上显示的特征?
    • 3D 特征是从源 'composite' source-layer = 'building' 和属性 extrude = 'true' 创建的,所以如果你有特征 id 你可以很容易地查询源 'composite '找到它。勾选默认example from mapbox
    • 我从地理编码 api 获得的特征看起来像这样address.10641720978437020,而 3d 建筑物的特征 ID 只是像 44900725 这样的数字。如何从地理编码 api 中获取 3d 建筑物的 ID?
    猜你喜欢
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 2019-11-22
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    相关资源
    最近更新 更多