Count Specific Overlays

Topics: Windows Forms
May 2, 2012 at 1:16 PM
Edited May 4, 2012 at 11:30 AM

Hi,

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.

Coordinator
May 2, 2012 at 2:19 PM

?

May 3, 2012 at 6: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.

Coordinator
May 3, 2012 at 6:58 AM

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

May 3, 2012 at 7:00 AM
Edited May 3, 2012 at 7: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?

Coordinator
May 3, 2012 at 7:07 AM

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

May 3, 2012 at 7:56 AM
Edited May 4, 2012 at 11:29 AM

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 8: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

Coordinator
May 3, 2012 at 8:40 AM
Edited May 3, 2012 at 8: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 8: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 4:19 PM
Edited May 4, 2012 at 11:29 AM

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"?

Coordinator
May 3, 2012 at 4: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 4: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 4:50 PM

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

May 3, 2012 at 4:54 PM
Edited May 4, 2012 at 11:29 AM

I believe I'm almost there:

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

Coordinator
May 3, 2012 at 5:41 PM

mytestoverlay.Markers.Remove

May 3, 2012 at 5:57 PM
Edited May 4, 2012 at 11:30 AM

Hmmmm....


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

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

May 3, 2012 at 6:23 PM
Edited May 4, 2012 at 11:30 AM

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

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

try:

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

May 3, 2012 at 7: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

Coordinator
May 3, 2012 at 7:15 PM

i don't code in vb, sorry ;}

May 3, 2012 at 7: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.

Coordinator
May 3, 2012 at 7:33 PM

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

May 3, 2012 at 7: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.

Coordinator
May 3, 2012 at 7:54 PM

debug, man ;}

May 3, 2012 at 8:08 PM

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

Coordinator
May 3, 2012 at 8:21 PM

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

May 3, 2012 at 8: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 9: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?

Coordinator
May 4, 2012 at 9: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)
{
mytestoverlay.Markers.Remove(m);

May 4, 2012 at 10:13 AM
Edited May 4, 2012 at 10: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
   mytestoverlay.Markers.Remove(m) 
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.

Coordinator
May 4, 2012 at 10:34 AM

maybe there are no markers in your overlay ;}

May 4, 2012 at 10:41 AM

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

May 4, 2012 at 11:11 AM

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 11:29 AM

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

Coordinator
May 4, 2012 at 12:49 PM

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

May 4, 2012 at 12:50 PM

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

Coordinator
May 4, 2012 at 1:04 PM

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

May 4, 2012 at 1:17 PM

Works great m8. ;}

Coordinator
May 4, 2012 at 1:22 PM

eureka ;}

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

 

 

 

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

 

May 31, 2012 at 1:02 PM

Hi all - I am also working in vb.net 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.

        Try

            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 2: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