Polygonsearch with C#

Anonymous
01-14-2005, 10:22 AM
Can anyone please help me with some C#-code on how to get a working polygon-search method...?

Wilfried
01-14-2005, 11:05 AM
Hi,

can you explain what you exacly means by "polygon-search method" ?

Anonymous
01-14-2005, 12:23 PM
I want to draw a polygon on the map with the freeform tool. The metod should for instance return all the pushpins in the area that the polygon encloses.

I want to be able to do a search within the polygon only, not the whole map.

Wilfried
01-14-2005, 01:28 PM
Hi,

Yu have to make triangles from out of the polygon (not a real problme of course but not coded in 5 minutes :( then check them each out invidually.

Anonymous
01-15-2005, 08:38 AM
I found this code in VB.NET. It should do the trick.


Dim a As MapPoint.Shape
Dim objDataSet As MapPoint.DataSet
Dim objRecordset As MapPoint.Recordset
ListBoxValdaKunder.Items.Clear()
If Karta.ActiveMap.Shapes.Count > 0 Then
objDataSet = Karta.ActiveMap.DataSets("sbg031_2003")
Cursor.Current = Cursors.WaitCursor
a = Karta.ActiveMap.Shapes(Karta.ActiveMap.Shapes.Coun t)
objRecordset = objDataSet.QueryShape(a)
If objRecordset.EOF = False Then
Do Until objRecordset.EOF
ListBoxValdaKunder.Items.Add(objRecordset.Pushpin. Name)
objRecordset.MoveNext()
Loop
End If
End If


I, however, can't figure out which dataset I should use when I translate this line into C#:
objDataSet = Karta.ActiveMap.DataSets("sbg031_2003")
Is there a way to find out which datasets I can choose from?

Wilfried
01-15-2005, 11:55 AM
Hi,

QueryShape returns all records within a shape (learned again something new). So when you add the pushpins to the map you add them as whell into a dataset. And itis _that_ dataset you have to use.

Anonymous
01-15-2005, 12:28 PM
So what is the default dataset? I haven't declared nor named any dataset in my application, and I'm still able to add pushpins to the map... so there must be som kind of default dataset?

Wilfried
01-16-2005, 03:17 AM
Hi,

I had a moment of free time so I did a little experiment, as I did not knew the answer for your problem eather. It seems that pushpins are always added to a "default" dataset. I assume it is always the first one in the collection. This is what I tryed:

uint Count = 10;
Location Loc;
Pushpin PP;
Double Lat = 50;
Double Lon = -3;
Double Alt = 1000;

while (Count-- > 0) {
Lon += 0.5;
Loc = MP.ActiveMap.GetLocation(Lat, Lon, Alt);
PP = MP.ActiveMap.AddPushpin(Loc, "Kieken");
PP.Symbol = 1;
}
MP.ActiveMap.DataSets.ZoomTo();

The ZoomTo works here ! This proves there is a dataset of the Pushpins. If there is one then we can access it. ps: dont mind the puspin's name, I always do silly things naming :) Lest just try something stupid and see where we coming. We draw a circle, select it ans see if we can get a recordset out of a dataset.

Loc = MP.ActiveMap.GetLocation(Lat, Lon, Alt);
Shape s = MP.ActiveMap.Shapes.AddShape(GeoAutoShapeType.geoS hapeRadius, Loc, 100, 100);
s.Line.Weight = 0;
s.Select();
object o = 1;
Recordset rs = MP.ActiveMap.DataSets.get_Item(ref o).QueryShape(s);
rs.MoveFirst();
while (!rs.EOF) {
Console.WriteLine(rs.Pushpin.Name.ToString());
rs.MoveNext();
}

And look here :) only the records in the shape are returned. I think this is what you need :) Note that the first dataset is 1 and not 0 in the index. This is with Mappoint whole time, it start indexing at 1.

Wilfried
01-16-2005, 04:26 AM
Hi,

I did a litte additional research and tought to put it here as it can benefit you and others.

First of all this dataset where pushpins are added has the name 'My Pushpins". I found this like this:

MapPoint.DataSets ds = MP.ActiveMap.DataSets;
foreach (MapPoint.DataSet d in ds)
Console.WriteLine(d.Name);
This means that you can access the selected pushpins by dataset name. Maybee it is also safer ? Or is index 1 always the 'My Pushpins" dataset ? Should be nice if someone canclarify this. Anyway as this you access it by name:

o = "My Pushpins";
rs = MP.ActiveMap.DataSets.get_Item(ref o).QueryShape(s);
rs.MoveFirst();
while (!rs.EOF) {
rs.MoveNext();
}
You can also create a new dataset and move the selected pushpins in it, like this. Note that they are deleted form the old dataset.

MapPoint.DataSet InCircle = MP.ActiveMap.DataSets.AddPushpinSet("In circle");
rs.MoveFirst();
while (!rs.EOF) {
rs.Pushpin.MoveTo(InCircle);
rs.MoveNext();
}
o = "In circle";
rs = MP.ActiveMap.DataSets.get_Item(ref o).QueryShape(s);
rs.MoveFirst();
while (!rs.EOF) {
rs.MoveNext();
}

 
Web mp2kmag.com
mapforums.com