我想添加一些可在地图上点击的图钉。首先,我想显示它们但是当我在地图上添加它们时,会发生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
答案 0 :(得分:1)
我尝试为地图上的每个点创建一个新的圆形实例,因为您无法将相同的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
};