mapbox - 我可以制作可点击的国家标签,但是当我点击其他任何地方时会出错
问题描述
当用户单击国家/地区标签时,我正在尝试创建一个弹出窗口。我的代码实现了这一点,但如果用户不小心点击了其他任何地方,我会收到错误“TypeError:无法读取未定义的属性'properties'”。任何帮助,将不胜感激。谢谢。
export default class App extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
lng: -32.87393,
lat: 43.6439,
zoom: 3,
mxZoom: 7,
};
this.mapContainer = React.createRef();
}
componentDidMount() {
const { lng, lat, zoom , mxZoom} = this.state;
const map = new mapboxgl.Map({
container: this.mapContainer.current,
style: 'mapbox://styles/chriswade112358/ckpipp5el0jky18qwzep8ppyl',
center: [lng, lat],
zoom: zoom,
minZoom: zoom,
maxZoom: mxZoom,
});
map.on('style.load', function() {
map.on('click', function(e) {
const features = map.queryRenderedFeatures(e.point, { layers: ['country-label'] });
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML('you clicked here: <br/>' + features[0].properties.name)
.addTo(map);
});
});
}
解决方案
推荐阅读
- java - Java-函数好像搞乱了控制台,调用函数后什么都不会打印出来
- excel - 获取公式的完整路径以打开工作簿
- java - 带有一个公共变量的 getter 和 setter 问题
- node.js - Webpack javascript Heap out of memory - 大量模块
- java - 在类之间传递变量 - java
- swift - RXSwift 更新单个元素
- c# - Xamarin 标签 IsVisible 属性没有改变
- azure-log-analytics - 如何将 Log Analytics 数据查询到 Azure 数据资源管理器?
- javascript - 操作不会更新商店
- android - 检查 NullPointer setVisibility 案例