简体   繁体   中英

Google Maps V3, how to get array of markers/overlays?

Now obviously if I was just adding them in a normal fashion I would just keep them in an array along the way but in my case the markers are being loaded in, but I am using a KML layer.

So after the KML loads and the markers show up I basically just want to know how many there are, and I can figure out some dumb hack for this (loading the KML file again with jQuery and counting it there or something), but ideally I just want something like myArray = map.getMarkers() or myArray = map.getOverlays() , does anything like that exist?

It seems such a simple task, not why it is taking me so long to find the answer. Maybe it just isn't possible?

没有这样的事情,您每次插入一个数组时都必须保留一个数组并添加对象ID。

I'm not sure if you're able to access the markers once you've added them in using a KLM layer. If you don't have that many markers, then I would say load them using the regular Marker class.

Maybe I didn't do it right, but last time I tried implementing markers using KLM layers I wasn't able to add any events to them, or access them at all.

If you only need to count them, then I would say load that same KML file using AJAX, and parse it as an XML and you can grab the elements from there to count them.

You can't access the markers in a KmlLayer (it is rendered as map tiles by Google's servers). You could use a FusionTableLayer (you can import KML into the FusionTable, then modify the query to that FusionTable to filter the markers) or a third party KML parser like geoxml3 or geoxml-v3 (which render the KML using native Google Maps v3 objects).

example using geoxml3

example using FusionTables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM