Search code examples
c#windows-phone-8bing-mapschildrenpushpin

Bing Maps foreach item in list add new pushpin


I'm trying to add multiple pushpins from a list to Bing Maps on Windows Phone. The name of each pushpin needs to be different, because I want to be able to remove them individually later using MainMap.Children.Remove(SpecificPushpin);.

This is my foreach:

Pushpin pushpin = new Pushpin();
Attractions attractions = new Attractions();
foreach (var attraction in Attractions.allAttractions)
{
    pushpin.GeoCoordinate = new GeoCoordinate(attraction.Latitude, attraction.Longtitude);
    pushpin.Content = attraction.Title;
    pushpin.Background = new SolidColorBrush(Colors.Blue);
    pushpin.Foreground = new SolidColorBrush(Colors.White);
    MainMap.Children.Add(pushpin);
}

Of course, I receive an error after the first loop through the foreach on the MainMap.Children.Add(pushpin); line, because "pushpin" is already an existing name.

I also tried using this:

MainMap.Children.Add(new Pushpin() { Content = attraction.Title, GeoCoordinate = new GeoCoordinate(attraction.Latitude, attraction.Longtitude), Background = new SolidColorBrush(Colors.Yellow), Foreground = new SolidColorBrush(Colors.Black) });

But then I will never be able to romove the pushpins individualy.

Does anyone know how I can give a variable name to each pushpin in my list, or knows another way to fix my problem?


Solution

  • Instead of using Bing Maps, I used the Map Control and worked with MapLayers and MapOverlay. I created a different MapLayer for each list class and removed those individually.

    This made it possible to remove one list of pushpins simply. Here's an example:

        private void LoadAttractions()
        {
            if (cbxAttractions.IsChecked != false)
            {
                Attractions attractions = new Attractions();
                foreach (var attraction in Attractions.allAttractions)
                {
                    Pushpin pushpin = new Pushpin();
                    pushpin.Name = attraction.Title;
                    pushpin.GeoCoordinate = new GeoCoordinate(attraction.Latitude, attraction.Longtitude);
                    pushpin.Content = attraction.Title;
                    pushpin.Background = new SolidColorBrush(Colors.Yellow);
                    pushpin.Foreground = new SolidColorBrush(Colors.Black);
    
                    MapOverlay MyOverlay = new MapOverlay();
                    mapLayerAttractions.Add(MyOverlay);
    
                    MyOverlay.Content = pushpin;
                    MyOverlay.GeoCoordinate = new GeoCoordinate(attraction.Latitude, attraction.Longtitude);
                    MyOverlay.PositionOrigin = new Point(0.0, 1.0);
    
                }
                MainMap.Layers.Add(mapLayerAttractions);
            }
        }
    

    And when cbxAttractions is being tapped:

    private void cbxAttractions_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (cbxAttractions.IsChecked == false)
            {
                MainMap.Layers.Remove(mapLayerAttractions);
            }
            else
            {
                LoadAttractions();
            }
        }