javascript - 如何在 Marker 中实现 mapbox 表达式?
问题描述
我怎样才能mapboxgl.Marker()
像这样在里面添加 Mapbox 表达式:
"icon-size": ['interpolate', ['linear'], ['zoom'], 10, 1, 15, 0.5]
(我从这里复制了这个表达式:https ://stackoverflow.com/a/61036364/5220885 )
解决方案
标记不支持表达式。它们是 HTML 元素,行为完全不同。
您将不得不按照以下方式伪造它:
- 将处理程序添加到地图的
zoom
事件中,以便您可以在地图放大和缩小时进行更新。 - 使用常规的旧 JavaScript 计算大小。
- 使用 CSS 应用该尺寸。
像这样的东西:
map.on('zoom', () => {
const scalePercent = 1 + (map.getZoom() - 8) * 0.4;
const svgElement = marker.getElement().children[0];
svgElement.style.transform = `scale(${scalePercent})`;
svgElement.style.transformOrigin = 'bottom';
});
Codepen 在这里:https ://codepen.io/stevebennett/pen/MWyXjmR?editors=1001
推荐阅读
- python - 为什么我的 Treeview 对象返回其项目的值之一而没有下划线?
- powershell - Grafana API 脚本创建新数据源失败
- python - argparse 库:PythonShell.py:错误:无法识别的参数
- routes - 除了上面的请求 DTO,有没有办法在其他地方声明路由?
- r - RM 功能不断出现在控制台上
- c# - Remove elements from List A that are in List B while keeping any duplicates in List A
- php - how to show warning of empty field on form validation?
- firebase - How to show just QuerySnapshots with boolean true in ListView? (Dart/Flutter)
- javascript - Asynchronous functions in an else block
- c++ - X11: How to get current top-left corner coordinates in visible area, when panning with mouse?