【发布时间】:2020-09-14 12:05:44
【问题描述】:
我认为没有解决方案,但也许我错了,或者将来有可能。
我正在尝试在给出确切地址或 GPS 坐标的情况下突出显示 mapbox(3d 或 2d)中的建筑物。
甚至可以在 mapbox 中唯一标识建筑物吗?
我在他们的documentation 中找不到任何东西,而且破解它似乎很困难,因为所有东西都画在一个画布上。
【问题讨论】:
标签: mapbox mapbox-gl-js
我认为没有解决方案,但也许我错了,或者将来有可能。
我正在尝试在给出确切地址或 GPS 坐标的情况下突出显示 mapbox(3d 或 2d)中的建筑物。
甚至可以在 mapbox 中唯一标识建筑物吗?
我在他们的documentation 中找不到任何东西,而且破解它似乎很困难,因为所有东西都画在一个画布上。
【问题讨论】:
标签: mapbox mapbox-gl-js
是的,这是可行的,即使是建筑物的一部分。在 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
});
}
});
【讨论】:
address.10641720978437020,而 3d 建筑物的特征 ID 只是像 44900725 这样的数字。如何从地理编码 api 中获取 3d 建筑物的 ID?