[Solved] Trouble with RegeneratePolygonShape offset

Topics: WPF
Oct 12, 2011 at 5:45 PM
Edited Oct 12, 2011 at 5:48 PM

Hi radioman, first of all, thanks for your cool work.

I'm using your component on a WPF project and I think I might be doing something wrong with the poly shape creation.

I've set up a basic map UI (just the map and a 'make map' button) and now I'm trying to draw 3 different polygons from my MakeMap() function like so:

 

                       GMapMarker m = new GMapMarker(new PointLatLng());
                        m.Polygon.Add(new PointLatLng(54, 25));
                        m.Polygon.Add(new PointLatLng(54, 50));
                        m.Polygon.Add(new PointLatLng(55, 20));
                       m.Polygon.Add(new PointLatLng(55, 30));
                        m.RegeneratePolygonShape(map);
                        map.Markers.Add(m);

                        GMapMarker z = new GMapMarker(new PointLatLng());
                        
                        z.Polygon.Add(new PointLatLng(64, -18));
                        z.Polygon.Add(new PointLatLng(65.80, -35));
                        z.Polygon.Add(new PointLatLng(67.199, -12));
                        z.Polygon.Add(new PointLatLng(64, -8));
                        z.RegeneratePolygonShape(map);
                        map.Markers.Add(z);

                        GMapMarker y = new GMapMarker(new PointLatLng());
                        
                        y.Polygon.Add(new PointLatLng(75.23, -24));
                        y.Polygon.Add(new PointLatLng(74.80, -35));
                        y.Polygon.Add(new PointLatLng(77.199, -12));
                        y.Polygon.Add(new PointLatLng(73.199, -12));
                        y.RegeneratePolygonShape(map);
                        map.Markers.Add(y);

 

From the coordinates I'm expecting one poly on the whereabouts of Iceland, one near Lithuania and one near Portugal/Spain. Instead all the polys get drawn on the whereabouts of central Africa :\

Now I did some fiddling around while I tried to locate the problem and came across the following inside the RegeneratePolygonShape():

var offset = Map.FromLatLngToLocal(Polygon[0]);

and then

 localPath.Add(new System.Windows.Point(p.X - offset.X, p.Y - offset.Y));

I can assume that this offset is used to compensate stuff like Zooming maybe? I then did some blunt and inconsiderate testing (by placing a 0,0 point on the [0] of the poly array), which caused the polys to be drawn on the expected locations (except for the 0,0 point of course). So in a nutshell, I'm assuming that the function is taking the first point for offset and applying that offset to all points, which explains the migrating polygons issue. Being again inconsiderate and rather desperate, I removed the offset calculation lines, this made the whole drawing process go bonkers, proving that the problem is in deed on my end.

So, what am I overlooking here? Is there an 'offset point' object or something that I haven't found? Do I have to set something for the [0] position on the poly array that is not a valid point?

On a side note, I went through the posts on the WPF discussion and I checked if the Forms version used the same process (in order to scour through the Forms discussion next), no dice on both resources.

Can you please help me out? Thanks in advance for reading.

 

 

 GMapMarker m = new GMapMarker(new PointLatLng());
                        m.Polygon.Add(new PointLatLng(54, 25));
                        m.Polygon.Add(new PointLatLng(54, 50));
                        m.Polygon.Add(new PointLatLng(55, 20));
                       m.Polygon.Add(new PointLatLng(55, 30));
                        m.RegeneratePolygonShape(map);
                        map.Markers.Add(m);

                        GMapMarker z = new GMapMarker(new PointLatLng());
                        // z.Polygon.Add(new PointLatLng(0, 0));
                        z.Polygon.Add(new PointLatLng(64, -18));
                        z.Polygon.Add(new PointLatLng(65.80, -35));
                        z.Polygon.Add(new PointLatLng(67.199, -12));
                        z.Polygon.Add(new PointLatLng(64, -8));
                        z.RegeneratePolygonShape(map);
                        map.Markers.Add(z);

                        GMapMarker y = new GMapMarker(new PointLatLng());
                        //  y.Polygon.Add(new PointLatLng(0, 0));
                        y.Polygon.Add(new PointLatLng(75.23, -24));
                        y.Polygon.Add(new PointLatLng(74.80, -35));
                        y.Polygon.Add(new PointLatLng(77.199, -12));
                        y.Polygon.Add(new PointLatLng(73.199, -12));
                        y.RegeneratePolygonShape(map);
                        map.Markers.Add(y);
                        GMapMarker m = new GMapMarker(new PointLatLng());
                        m.Polygon.Add(new PointLatLng(54, 25));
                        m.Polygon.Add(new PointLatLng(54, 50));
                        m.Polygon.Add(new PointLatLng(55, 20));
                       m.Polygon.Add(new PointLatLng(55, 30));
                        m.RegeneratePolygonShape(map);
                        map.Markers.Add(m);

                        GMapMarker z = new GMapMarker(new PointLatLng());
                       
                        z.Polygon.Add(new PointLatLng(64, -18));
                        z.Polygon.Add(new PointLatLng(65.80, -35));
                        z.Polygon.Add(new PointLatLng(67.199, -12));
                        z.Polygon.Add(new PointLatLng(64, -8));
                        z.RegeneratePolygonShape(map);
                        map.Markers.Add(z);

                        GMapMarker y = new GMapMarker(new PointLatLng());
                      
                        y.Polygon.Add(new PointLatLng(75.23, -24));
                        y.Polygon.Add(new PointLatLng(74.80, -35));
                        y.Polygon.Add(new PointLatLng(77.199, -12));
                        y.Polygon.Add(new PointLatLng(73.199, -12));
                        y.RegeneratePolygonShape(map);
                        map.Markers.Add(y);
Coordinator
Oct 12, 2011 at 5:58 PM
Edited Oct 12, 2011 at 6:01 PM

 GMapMarker y = new GMapMarker(new PointLatLng()); // --> y = new GMapMarker(new PointLatLng(0.0, 0.0)); // and that is the Africa ;}

Oct 12, 2011 at 6:10 PM
Edited Oct 12, 2011 at 6:10 PM

Ooooh *stares in awe at code snippet*, so I need to start setting points right from the begging, like so:

 

                        GMapMarker m = new GMapMarker(new PointLatLng(54, 25));
                        m.Polygon.Add(new PointLatLng(54, 50));
                        m.Polygon.Add(new PointLatLng(55, 20));
                       m.Polygon.Add(new PointLatLng(55, 30));
                        m.RegeneratePolygonShape(map);
                        map.Markers.Add(m);

                        GMapMarker z = new GMapMarker(new PointLatLng(64, -18));
                        z.Polygon.Add(new PointLatLng(65.80, -35));
                        z.Polygon.Add(new PointLatLng(67.199, -12));
                        z.Polygon.Add(new PointLatLng(64, -8));
                        z.RegeneratePolygonShape(map);
                        map.Markers.Add(z);

                        GMapMarker y = new GMapMarker(new PointLatLng(75.23, -24));
                        y.Polygon.Add(new PointLatLng(74.80, -35));
                        y.Polygon.Add(new PointLatLng(77.199, -12));
                        y.Polygon.Add(new PointLatLng(73.199, -12));
                        y.RegeneratePolygonShape(map);
                        map.Markers.Add(y);

 

This is the correct way to do it then?

Thanks for the help!

Coordinator
Oct 12, 2011 at 6:43 PM

yes, you create marker, set the position, generate the shape, and add it to the map

Oct 13, 2011 at 9:54 AM

Thanks for your time and for the help, problem solved!