【问题标题】:How to get properties from feature without event listener? (event.feature)如何在没有事件侦听器的情况下从功能获取属性? (事件.特征)
【发布时间】:2016-06-16 15:14:35
【问题描述】:

如何在没有事件监听器的情况下从层(加载了 geoJSON)获取“特征”数据?

这是通常的方式,效果很好:

layer1.addListener('mouseover', function (event) {
    console.log( event.feature.getProperty('description') );
}

但现在我想从数据对象“layer1”中获取值。

我已经试过了:

layer1.getProperty('description')
layer1.feature.getProperty('description')

我加载 geoJSON 的方式

var layer1 = new google.maps.Data();
layer1 = loadGeoJson('https://googledrive.com/host/MYFILE')

json的内容

顺便问一个简短的问题:如果我的 json 中有更多特征(例如多边形),我可以获取这些值来操纵它(例如切换可见性)吗?

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    12.3456789,
                    01.2345678,
                    0
                ]
            },
            "properties": {
                "name": "Test Polygon",
                "description": "My Test Description"
            }
        }
    ]
}

【问题讨论】:

    标签: javascript json google-maps google-maps-api-3


    【解决方案1】:

    要获得google.maps.Data 层的所有功能,请使用:

    forEach(callback:function(Data.Feature))

    返回值:无

    重复调用给定函数,在每次调用时将集合中的特性传递给函数。特征的迭代顺序是不确定的。

    或获取单个功能(如果您知道 Id):

    getFeatureById(id:number|string)

    返回值:Data.Feature|undefined

    如果集合中存在具有给定 ID 的特征,则返回该特征。否则返回未定义。

    请注意,ID 1234 和“1234”是等效的。两者都可用于查找相同的特征。

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多