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

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?

There's no such thing, you have to keep an array and add the object IDs everytime you insert one.

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

Related

Specific to angular Google Maps: clearing out markers from map

This is an Angular specific / Google Maps problem. I am using the Angular Google Maps library. I am struggling to reference Google's Marker object because they are generated in the HTML file.
HTML
<map-marker
#markerElem
*ngFor="let marker of markers"
[position]="marker.position"
[title]="marker.title"
[options]="marker.options"
(mapClick)="openInfo(markerElem, marker.info)"
>
</map-marker>
I need to clear these markers out that are generated in this HTML file. It seems that google maps does not have a clearMap() functionality, and most forums agree it is best to keep track of markers by having a list of them in the code.
The "markers" variable seen in the HTML is just a custom object I made to fill in the fields of the actual Google Marker object.
There are two solutions I can think of but I don't know how to go about doing it.
1.) generate the Marker objects in my TS file. (Note: I would like to keep as much as possible the structure of the HTML. One thing I did worry is that the tag would automatically create a map marker and thus my list of Marker objects wouldn't actually be the ones on the map).
2.) somehow reference the (I think these tags actually generate markers in the background) as a list of Markers.
If anyone has examples of clearing markers off the map in Angular please share. Any help / suggestions are appreciated.
When you need to clear the markers,
In your ts file, just do
markers = [];

Get existing Google Maps V3 instance?

I am using a WordPress plugin for a Store Locator, and it does a lot of good things. But there are a few things that I need to tweak and I do not want to modify the plugin's code. I was wondering if there was any way to get ahold of the current map that it outputs and all of its settings so that I could manipulate them, or add to them? I would do this in my own JS file.
The ID of the div that contains the map is #wpsl-gmap and one thing I want to do is after loading the results I need to trigger a resize on the map because the area that it is loaded into increases and the tiles do not load for the new section. I would also like to center the map if possible.

Antialising in KML layer in Google Maps API

I have a map created in My Maps, and I wand to embed it in a website, however I'd like to have InfoWindows on marker click instead of the default side panel, and change the description a bit.
From what I found, the only way to do so is to use a regular Google Maps API and add my data onto it with a KML layer. This is working, however, the icons (the pins) are highly pixelated and look terrible. When using the same map via embed (or simply opening it) they are OK.
Would really appreciate any help with the issue. Probably there is another way of adding a custom map to an API-powered map?
The answer I've come up with is: no way to achieve what I'm looking for.
When exporting KML layers, Google scales all icons to 32X32 px, and then scales them up (kml file contains scale 1.1 directive). Even if you change the scale, the files remain 32X32, so it does not help; you need to create another files, in other words: the kml exported from Google is not usable if you have custom png icons.
I ended up using regular markers instead of KML layer.

arcgis javascript how can i turn off layers

i am doing the tutorial for displaying the map in a web browser
https://developers.arcgis.com/javascript/3/jshelp/intro_agstemplate_amd.html
now I have the code, what if I don't want to display all the layers, and want to turn off the first layer?
i can do it by index of the layer, or can i use the layer name?
trying to understand i got to https://developers.arcgis.com/javascript/3/jsapi/arcgisdynamicmapservicelayer-amd.html but not sure how to use javascript
thanks guys.
If you want to turn off and on layers manually, I would use the layerlist widget script LayerList widget
or if you need the option to reorder the layers with the ability to turn layers on or off manually I would use the Reorder layers in map service

Swiching between two sets of markers with Google Maps

I am using Google Maps Javascript API and in my website I would like to show people and places but not at the same time. The user should be able to switch from first set of people markers to a set of places markers.
I can simply remove all the people markers and then put all the places markers but it seems like there is another way to do the switching using the OverlayView class.
How does it work ?
It might first seem clumsy but markers itself are overlays and removing and displaying them "by the book" is done keeping references to them using array and looping through setting for example marker.setMap(null) I digged out for you docs example which demonstrates well how you need to play with them. I have used them in a past (displaying and removing) never faced real performance issues, its relatively fast.
Key is not to delete them completely until you want, just setting their map null removes them from the map. Always keep them in a array if you need to display them again. For your case I would either made two arrays of markers or just one array where markers has parameter such as marker.myType="people" which you can use to check against when looping and doing things.

Categories

Resources