Count Specific Overlays

Topics: Windows Forms
May 2, 2012 at 2:16 PM
Edited May 4, 2012 at 12:30 PM


How do I count all overlays with the name (for example): overlay_one.

' removed, code not correct

As you can see I'm adding multiple overlay_one 's to the map. How do I count all of them? I don't want all the overlays (mymap.overlays.count) as I do now, but only to count the "overlay_one".


Thanks in advance.

May 2, 2012 at 3:19 PM


May 3, 2012 at 7:36 AM

Okay... another try:

Each time I click a button I'm adding a overlay/marker to the map. The overlay needs to have a unique name. like "Marker10". When I add a route that also need to have a unique name like "Route1". I want them to have a unique name so I can remove them by name instead of all of them at once. In the example above I'm incrementing.

- route1
- route2
- route3
- route4
- route5

- location1
- location2
- location3
- location4
- location5

I hope I made it a bit more clear.

May 3, 2012 at 7:58 AM

so whats the problem, assign unique name, and remove by it

May 3, 2012 at 8:00 AM
Edited May 3, 2012 at 8:02 AM

The problem is that I don't know how to get the overlays which have "location" in the name/id. That way I can also remove all the "locations" (only) in one time. In the example in the OP I search all overlays, but that is not the correct way. I only need to count the "locations" or the "routes". How?

May 3, 2012 at 8:07 AM

assign unique name/tag for route/marker ;} there are no miracles in this matter

May 3, 2012 at 8:56 AM
Edited May 4, 2012 at 12:29 PM

As you can see I'm already assigning unique names or am I missing something?



The problem is that I don't know HOW to count the "mplace_overlay" before I increment the number. It is also possible that I'm using the wrong approach.

May 3, 2012 at 9:36 AM

Hello radjesh,

you can add multiple markers, routes and polygons in one overlay. Do you really need so much overlays?

After adding a marker, just invoke this.mymap.invoke() to refresh rendering.

To count them: this.myoverlay.markers.count(), this.myoverlay.routes.count(), this.myoverlay.polygons.count().

is this the answer for your question?

bye EDE

May 3, 2012 at 9:40 AM
Edited May 3, 2012 at 9:42 AM

why do create separate overlay for each marker?

p.s. map.Invalidate(); // lazy-async refresh, map.Refresh(); for instant refresh

May 3, 2012 at 9:40 AM

Hi EDE, so it seems that I'm taking the wrong approach here. Let me give it a try and I'll let you know. Thanks for bringing this up to me ;)

May 3, 2012 at 5:19 PM
Edited May 4, 2012 at 12:29 PM

Getting closer. Thanks for the tips.

Now I have only one overlay with a marker.

As example the tag of the marker = "test1"

Which property do I need to use to remove (besides remove) the marker where the tag is "test1"?

May 3, 2012 at 5:30 PM

well if you set marker.Tag, then the basic logic dictates to use the same marker.Tag for comparation ;}

May 3, 2012 at 5:46 PM

??? I believe in my example you see that I don't know how to compare the tag. I already know that I need to lookup the marker.tag, but how.

May 3, 2012 at 5:50 PM

wait..... an idea came up...

May 3, 2012 at 5:54 PM
Edited May 4, 2012 at 12:29 PM

I believe I'm almost there:

I can hide it though... :lol:  Okay.... how to remove it?

May 3, 2012 at 6:41 PM


May 3, 2012 at 6:57 PM
Edited May 4, 2012 at 12:30 PM


Collection was modified; enumeration operation may not execute.
May 3, 2012 at 7:07 PM

var m = mytestoverlay.Markers.FirstOrDefault(p => (p.Tag as string) == "test1");
if(m != null)

May 3, 2012 at 7:23 PM
Edited May 4, 2012 at 12:30 PM

Nothing is happening. Also debugged. This is my full code:

' removed, code not correct
May 3, 2012 at 7:51 PM


TryCast(p.Tag, String).Equals("test1")

May 3, 2012 at 8:12 PM

Function(p) TryCast(p.Tag, String).Equals("1"))

Object reference not set to an instance of an object.

p          {GMap.NET.WindowsForms.Markers.GMarkerGoogle}    GMap.NET.WindowsForms.GMapMarker
p.Tag    1 {Integer}                                                                  Object

May 3, 2012 at 8:15 PM

i don't code in vb, sorry ;}

May 3, 2012 at 8:19 PM

No problem, but I can convert the c# to vb. Does the code work in C#? Seems it isn't that easy to remove a marker from the map.

May 3, 2012 at 8:33 PM

;} check the demos, the vehicle or tcp/ip live visualization, works like charm, perfectly

May 3, 2012 at 8:49 PM

Don't have C# m8. ;)

But if your code works in C# it should also in VB, but it doesn't. I've used 4 different converters and they all give the same result.

May 3, 2012 at 8:54 PM

debug, man ;}

May 3, 2012 at 9:08 PM

Hahahaha, c'mon. Already told you that debugging gives me no result. I always try my best before asking.

May 3, 2012 at 9:21 PM

it's not gmap issue, just plain coding/debuging

May 3, 2012 at 9:27 PM

Yeah, I figured. ;) I'm asking this on another forum if someone has experience with this. Will let you know.

May 3, 2012 at 10:02 PM

Forgot to mention that m returns "nothing"

I've looked in the source code, but can't find where you applied this. Can you kick me to the proper .cs file (and perhaps linenumber) so I can have a closer looksee?

May 4, 2012 at 10:40 AM

try this if you have some markers with unassigned tag, it won't throw exception:

var m = mytestoverlay.Markers.FirstOrDefault(p => p != null && (p.Tag as string) == "test1");
if(m != null)

May 4, 2012 at 11:13 AM
Edited May 4, 2012 at 11:34 AM

All the markers are getting a tag, so there aren't unassigned tags. I'm counting the markers and adding it to the tag:

mytestmarker.Tag = mytestoverlay.Markers.Count + 1
mytestmarker.ToolTipText = mPlace_marker.Tag.ToString

 Dim m As GMapMarker = mark_overlay.Markers.FirstOrDefault(Function(p) p IsNot Nothing AndAlso TryCast(p.Tag, String) = "1")
If m IsNot Nothing Then
End If 


 Tested your code as you can see in code 2, but nothing happens. Debugged it:

Tag  |  Nothing    |   Object
m     |  Nothing    |   GMap.NET.WindowsForms.GMapMarker

The weird thing is that the code makes absolutely sense, but nothing happens... Let me make another project and uplload it so you can see what I'm doing exactly.

May 4, 2012 at 11:34 AM

maybe there are no markers in your overlay ;}

May 4, 2012 at 11:41 AM

Hahahaha. If it were that simple ;)  Added a messagebox to count the markers on the layer > result = 1

May 4, 2012 at 12:11 PM

There seems to be issues with my application. :cry:

The code works perfectly. Will investigate. Thanks for all the help and patience.

May 4, 2012 at 12:29 PM

The tag of the marker needs to be a string ;) One small detail... Damn!!!

May 4, 2012 at 1:49 PM

not necessary, you can use numbers, but then in search function, use number too ;}

May 4, 2012 at 1:50 PM

Hmmmm. Then I still don;t understand why it didn't work. But hé, I'm saved ;)

May 4, 2012 at 2:04 PM

var m = mytestoverlay.Markers.FirstOrDefault(p => p != null && (int)p.Tag == 1);
if(m != null)

May 4, 2012 at 2:17 PM

Works great m8. ;}

May 4, 2012 at 2:22 PM

eureka ;}

May 31, 2012 at 1:44 PM
Edited May 31, 2012 at 1:47 PM




May 31, 2012 at 1:44 PM
Edited May 31, 2012 at 1:47 PM


May 31, 2012 at 2:02 PM

Hi all - I am also working in and trying to ascertain how I can identify the id of a specific marker that I want to delete

When I make the marker, I give it a mymarker.tag of "mymarkertag"

I also set mymarker.tooltiptext = mymarker.tag and the display suggests that the tag has been allocated as I would expect.

Using a derivation of radioman's last post, I try to find the id of the marker whose tag is "mymarkertag" but the result is that I provoke the message that indicates that no such marker was found. I have referenced some of the source code but because it's c# I'm struggling so any pointers would be massively appreciated.


            markerindex = rco.Markers.FirstOrDefault(Function(p) p IsNot Nothing AndAlso (p.Tag = "mymarkertag"))
            MsgBox("The marker whose tag is mymarkertag is index " & markerindex)
        Catch ex As Exception
            MsgBox("Failed to identify a marker with a tag of mtmarkertag")

        End Try




May 31, 2012 at 3:25 PM

Cancel that - whilst it may not be the most elegant, I just looped round all of the markers until I encountered one with the tag I was interested in