google-maps - 在地图 API 中将点击处理程序添加到 geojson
问题描述
有没有办法将 GeoJson 与点击事件相关联?
const features = map.data.addGeoJson(json);
for (const feature of features) {
// Add styling
map.data.overrideStyle(
feature,{
fillColor: 'red',
});
// TODO: Add a click handler for "feature"
}
对于普通实体,例如LatLngBounds
,似乎有addListener(instance, eventName, handler)
。但是,上面代码片段中的功能不存在此功能。
geojson 功能是否有等效的替代方案?
解决方案
没关系,我刚刚找到了这个帮助页面,它提供了一个关于如何做到这一点的片段:
// Set mouseover event for each feature.
map.data.addListener('mouseover', function(event) {
// "event" has the feature as a property.
document.getElementById('info-box').textContent =
event.feature.getProperty('letter');
});
推荐阅读
- gmail-addons - G Suite 中有哪些不同的插件类型?
- java - Scala 在使用 Lift Web 提取 json 值时抛出错误
- python - 我需要从 python 中 Colebrook 方程的近似值中输入摩擦系数
- linux - 仅在某些子目录中查找
- c# - ASP.NET Core 3.1 OData PageResult 序列化
- android - 你能告诉我如何添加这个活动吗?
- java - Java 程序无法编译:缺少返回语句
- javascript - 在带有 PKCE 的 OAuth 授权流中使用时,如何在 Azure 应用注册中启用 CORS?
- excel - 如果范围内的单元格不等于空白,则将字符串应用于另一个范围内的单元格
- java - 带有 fcm 的 Android 网页视图