在地图上添加多个坐标时抛出

本文关键字:坐标 地图 添加 | 更新日期: 2023-09-27 18:11:44

我想添加一些图钉,可以在地图上点击。首先,我想显示它们,但是当我在地图上添加它们时,发生了ArgumentException,我的应用程序崩溃了。如果我在地图上只添加一个地方,它可以工作,但当我试图添加更多地方时,它就崩溃了。整个列表已遍历。

我代码:

var myCircle = new Ellipse
                {
                    Fill = new SolidColorBrush(Colors.Blue),
                    Height = 20,
                    Width = 20,
                    Opacity = 50
                };
MapLayer locationLayer = new MapLayer();
foreach (var place in r.Result)
                {
                    //It's a method that I created to get the placecoordinate in good format because it can be with commas
                    var placeCoordinate = Geolocalisation.GetCoordinateInGoodFormat(place.Google_lat,
                                                                                    place.Google_lng);
                    if (placeCoordinate == null)
                    {
                        continue;
                    }
                    var locationOverlay = new MapOverlay
                        {
                            Content = myCircle,
                            PositionOrigin = new Point(0.5, 0.5),
                            GeoCoordinate = placeCoordinate
                        };
                    Debug.WriteLine(place.Title + ", lat: " + place.Google_lat + ", long: " + place.Google_lng);
                    //Display e.g.: soleil du midi, lat: 50.8382836, long: 4.3975321
                    locationLayer.Add(locationOverlay);
}
mapControl.Layers.Add(locationLayer); //my map in XAML

错误:

An exception of type 'System.ArgumentException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary

在地图上添加多个坐标时抛出

我已经尝试为地图上的每个点创建一个圆圈的新实例,因为你不能将相同的UIElement(在这种情况下是circle)添加到视觉树两次。

      var locationOverlay = new MapOverlay
            {
                    Content = new Ellipse()
                            {
                                    Fill = new SolidColorBrush(Colors.Blue),
                                    Height = 20,
                                    Width = 20,
                                    Opacity = 50
                            },
                    PositionOrigin = new Point(0.5, 0.5),
                    GeoCoordinate = placeCoordinate
            };