PDA

View Full Version : Looping through Vertices of PolyLine?



OOT
12-12-2006, 06:49 AM
Well, this is my first post of what I am sure will be many. I've just started using Mappoint and thanks to the many good examples here I've been able to get a couple of polygons drawn programmatically onto the screen. Much Excedrin has been taken :)

I am attempting to loop through the vertices of the polygon and output their coordinates to the Immediate Window. Oh, sorry. I'm using Visual Basic 2005 and Mappoint 2006. I am also VERY new to VB 2005 and OOP ... needless to say there is lots of trial and error and sometimes code works and I don't know why.

Right now there is only 1 polygon on the screen. It has 10 vertices. So, to output those to the console I use this loop:

' loop through polygon and output the lat and lng for each of it's points
Dim arrSys As System.Array
Dim ienumSysColl As System.Collections.IEnumerator
Dim intPointCount As Long
Dim objLoc As MapPoint.Location
Dim objShape As MapPoint.Shape

For Each objShape In objMap.Shapes

arrSys = objShape.Vertices
ienumSysColl = arrSys.GetEnumerator
intPointCount = UBound(objShape.Vertices)

For i = 0 To intPointCount

ienumSysColl.MoveNext()
objLoc = ienumSysColl.Current

Console.WriteLine("Point " & i & _
": lat: " & objLoc.Latitude & _
" lng:" & objLoc.Longitude)

Next

NextThis works but I can't help but think there has to be a cleaner, more efficient way of accomplishing this task. A For Loop inside of a For Loop? One thing I think would help clean it up would be if I could just say "use that polygon" ... instead of looping through each Polygon (which right now isn't a problem because there is only one).

Is there a better, more efficient way of doing this? Please remember that I am very new to both VB 2005, OOP and Mappoint so if you can help please explain things to me like I'm 10 years old ;). Any and all help is greatly appreciated.

Wilfried
12-12-2006, 10:05 AM
Hi,

Your approach seems fine :)
You have a for loop in a for loop because foreach is like a loop. Only consider that foreach does not garantie the sequence (depending how the interface is written). So you code seems fine !

As for OOP you have to think an object as a black box. An object has inputs (properties, methods), and outputs (events, properties). An well written object does not now about objects it does not belong to or way around. Whit that in mind you are thinking OOP :)

OOT
12-12-2006, 12:36 PM
Hi,

Your approach seems fine :)
You have a for loop in a for loop because foreach is like a loop. Only consider that foreach does not garantie the sequence (depending how the interface is written). So you code seems fine !

As for OOP you have to think an object as a black box. An object has inputs (properties, methods), and outputs (events, properties). An well written object does not now about objects it does not belong to or way around. Whit that in mind you are thinking OOP :)
Wow. Thank you for the answer. I was expecting laughter and ridicule about my code. It does seem to be working. I will have to figure out some way of letting the user select the polygon before retrieving the vertices (don't need all of them if they only want the one's from one polygon).

Thank you also for the explanation on how to approach OOP. I keep reading different tutorials but most seem to think that you're reading the tutorial because you already understand OOP - definitely not the case with me.

Thank you again for the help and the advice on how to approach it. It is very much appreciated.

Wilfried
12-12-2006, 01:11 PM
Hi,

You can use SelectionChange event to check what is selected.

About the OOP I dont know a good tutorial.

1 advantage of using objects that have multiple instances is: The code is only 1 time in memory, only the data is there multiple times. That is because the first argument of an object method is a pointer to the object data itself, so the code uses this pointer and offsets from it to have it's own data.

If you dont need multiple instances then choose from what you wants an separate object to have readable, understeandable code for happy maintenance later :)

Dont use cross references. Make an object as a "black box". You put something in it and it give you something back, for example in an event. Very easy for re-use in other projects then.