You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Maps API:如何为数据层中的Point添加自定义数据

解决Google Maps Data Layer添加自定义数据的问题

你之前的写法无效,核心原因是Google Maps Data Layer的**要素(Feature)**要求自定义数据必须放在properties对象里,而不是直接和geometry字段平级。

正确的写法示例

把自定义的学校名称嵌套进properties对象中,修改后的代码如下:

var schoolLayer = new google.maps.Data(); 
schoolLayer.setMap(map); 
let latLng = new google.maps.LatLng(coords1, coords2); 
let school = new google.maps.Data.Point(latLng); 
// 自定义数据必须放在properties对象内
schoolLayer.add({
  geometry: school,
  properties: {
    school_name: 'Hello Elementary School'
  }
}); 

如何读取自定义数据

后续如果需要调用这些属性(比如点击点位时显示学校名称),可以用getProperty()方法:

schoolLayer.addListener('click', function(event) {
  // 获取自定义的学校名称属性
  let schoolName = event.feature.getProperty('school_name');
  // 这里可以扩展为弹窗展示、信息窗口渲染等操作
  alert('当前学校:' + schoolName);
});

补充说明

Google Maps Data Layer的要素结构遵循GeoJSON规范,所有非几何类的自定义属性都必须嵌套在properties字段下,直接和geometry同级的自定义字段会被系统忽略,这就是你之前的写法不生效的原因。

内容的提问来源于stack exchange,提问作者Valachio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:31:23