WPF必应地图如何以特定多边形为中心?

时间:2019-01-28 18:35:48

标签: c# wpf bing-maps

我已经基于导入的.gpx文件创建了多边形。

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一切正常,我已经在坐标表中加载了一个dataGrid,现在我想要的是当我在dataGrid中选择一个Track Name时,将Map定位在具有相同Name的Polygon的中心。 有人有什么想法吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

最简单的选择是计算最小和最大纬度和经度值,然后使用它来创建一个边界框(LocationRect),然后使用它来设置地图视图。这将为您处理缩放级别和居中。

这不适用于穿过反子午线(经度-180/180)的多边形。为了解决这个问题,有一种更复杂的方法,涉及更多的数学运算。

相关问题