2013-05-02 26 views
0

我想添加一些可以在地圖上點擊的圖釘。首先,我想顯示它們,但是當我將它們添加到地圖上時,發生ArgumentException,並且我的應用程序崩潰。 如果我只在地圖上添加一個地方,它可以工作,但是當我嘗試添加更多地方時,它會崩潰。整個列表已被遍歷。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 

回答

1

我曾嘗試創建圈子在地圖上的每一個點的新實例,因爲你不能添加相同的UIElement(在這種情況下,圓)兩次到視覺樹。

 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 
      };