如何从没有事件侦听器的图层(加载了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"
            }
        }
    ]
}

最佳答案

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


  forEach(callback:function(Data.Feature))
  
  返回值:无
  
  重复调用给定的函数,在每次调用时将集合中的功能传递给该函数。功能的迭代顺序是不确定的。


或获得一个功能(如果您知道ID):


  getFeatureById(id:number | string)
  
  返回值:Data.Feature |未定义
  
  返回具有给定ID的功能(如果它存在于集合中)。否则返回未定义。
  
  请注意,ID 1234和“ 1234”是等效的。可以用来查找相同的功能。

关于javascript - 如何从没有事件监听器的功能中获取属性? (event.feature),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37863136/

10-12 00:30
查看更多