0
votes

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?

2
make pushpin object inside the foreach loop.Ashok Damani
By doing that I still won't be able to remove certain pushpins, right? In the same foreach, I tried doing this: if (AppSettings.Contains("cbxAttractionChecked")) { MainMap.Children.Remove(pushpin); } It gets in that if when the user unchecks a checkbox. This doesn't work though.user3478148

2 Answers

1
votes

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();
        }
    }
0
votes

You can give each your Pushpin a property, for example:Tag. And Each tag is not same to another. So when you remove a specific pushpin. You can use foreach like this:

foreach(PushPin pushpin in yourPushpinList)
{
    if (pushpin.Tag == yourValue)
    {
        MainMap.Children.Remove(SpecificPushpin);
        break;
    }
}

the code maybe not correct, it is just a example.