Inuyasha Sama
Inuyasha Sama

Reputation: 11

WPF Bing Map How to center on specific polygon?

I have created polygons based on imported .gpx file.

public void LoadGPXCoordonate2(string sFile)
    {
        XDocument gpxDoc = GetGpxDoc(sFile);
        XNamespace gpx = GetGpxNameSpace();
        string comand = "INSERT INTO Coordinate_Import_Table (TI_ID,Latitude,Longitude) VALUES (@ti_id,@latitude,@longitude)";
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd2 = new SqlCommand(comand, conn);

        SqlParameter TiId = new SqlParameter("@ti_id", SqlDbType.Int);
        cmd2.Parameters.Add(TiId);
        SqlParameter LAtitude = new SqlParameter("@latitude", SqlDbType.Float);
        cmd2.Parameters.Add(LAtitude);
        SqlParameter LOngitude = new SqlParameter("@longitude", SqlDbType.Float);
        cmd2.Parameters.Add(LOngitude);
        conn.Open();
        cmd2.Prepare();
        var tracks = from track in gpxDoc.Descendants(gpx + "trk")
                     select new
                     {
                         Name = track.Element(gpx + "name") != null ?
                        track.Element(gpx + "name").Value : null,
                         Segs = (
                            from trackpoint in track.Descendants(gpx + "trkpt")
                            select new
                            {
                                Latitude = trackpoint.Attribute("lat").Value,
                                Longitude = trackpoint.Attribute("lon").Value,

                            }
                          )
                     };
        float lat;
        float lon;
        string name;
        foreach (var trk in tracks)
        {

            MapPolygon polygon = new MapPolygon();
            polygon.Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Blue);
            polygon.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Green);
            polygon.StrokeThickness = 3;
            polygon.Opacity = 0.5;
            polygon.Focusable = true;
            polygon.Locations = new LocationCollection();
            name = trk.Name.ToString();
            polygon.Name = name;
            polygon.ToolTip = name;
            foreach (var trkSeg in trk.Segs)
            {


                FillComboBox(name);
                lat = float.Parse(trkSeg.Latitude);
                lon = float.Parse(trkSeg.Longitude);

                Location loc = new Location(lat, lon);
                polygon.Locations.Add(loc);

                int tiid = int.Parse(comboBox.SelectedValue.ToString());
                TiId.Value = tiid;
                LAtitude.Value = lat;
                LOngitude.Value = lon;
                cmd2.ExecuteNonQuery();
            }
            myMap.Children.Add(polygon);
        }
        conn.Close();

    }

Everythig works fine.I have loaded a dataGrid with the coordinates table.What I want now is when I Select a Track Name in the dataGrid, the Map to center on the Polygon that has the same Name. Does anyone have any ideea how can I do that? Thank you!

Upvotes: 1

Views: 366

Answers (1)

rbrundritt
rbrundritt

Reputation: 18052

The simplest option is to calculate the min and max latitude and longitude values and then use that to create a bounding box (LocationRect) and then use that to set the map view. That will handle both the zoom level and the centering for you.

This won't work well for polygons that cross the anti-meridian (-180/180 longitude). To handle that there is a much more complex approach involving a lot more math.

Upvotes: 1

Related Questions