mapbox 更改高亮 3d 建筑
mapbox change highlight 3d building
我认为没有解决这个问题的方法,但也许我错了或者将来有可能。
我正在尝试在给定确切地址或 GPS 坐标的情况下在 mapbox(3d 或 2d)中突出显示建筑物。
甚至可以在 mapbox 中唯一标识建筑物吗?
我在他们的 documentation 中找不到任何东西,而且破解它似乎很困难,因为所有东西都画在一个 canvas 上。
是的,这是可行的,甚至是建筑物的一部分。在 Mapbox 中表示为建筑物的任何事物都基于 1 个或多个特征。为此,您需要查询功能,然后决定您将使用什么属性来仅过滤某些功能以区分行为。
检查一下,它是 how to change color and pointer on mouse over 上的一个 fiddle,我在其中添加了一个过滤器,仅针对纽约市所有建筑物中的一个特征 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 坐标的情况下在 mapbox(3d 或 2d)中突出显示建筑物。
甚至可以在 mapbox 中唯一标识建筑物吗?
我在他们的 documentation 中找不到任何东西,而且破解它似乎很困难,因为所有东西都画在一个 canvas 上。
是的,这是可行的,甚至是建筑物的一部分。在 Mapbox 中表示为建筑物的任何事物都基于 1 个或多个特征。为此,您需要查询功能,然后决定您将使用什么属性来仅过滤某些功能以区分行为。
检查一下,它是 how to change color and pointer on mouse over 上的一个 fiddle,我在其中添加了一个过滤器,仅针对纽约市所有建筑物中的一个特征 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
});
}
});