Evilunclebill
Evilunclebill

Reputation: 789

Adding multiple pushpins to bing map

I am trying to add multiple pushpins to a bing map in a windows 10 app. I face the trouble that it is only the last added lat, long that gets added. Let me post my code:

map.xaml:

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Hamburger_Heaven_Challenge"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Maps="using:Windows.UI.Xaml.Controls.Maps"
x:Class="Hamburger_Heaven_Challenge.Map"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Maps:MapControl x:Name="MyMap" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="700" Width="1260">

    </Maps:MapControl>
</Grid>

map.xaml.cs:

    public sealed partial class Map : Page
{
    PushPin pushPin = new PushPin();

    public Map()
    {
        this.InitializeComponent();

        AddPushPins();
        AddIcon();

        MyMap.Center = new Geopoint(new BasicGeoposition() {Latitude = 46.8442643, Longitude = 2.5992004 });
        MyMap.ZoomLevel = 6;

    }

    public void AddPushPins()
    {
        pushPin.AddPushPin(46.8442643, 2.5992004);
        pushPin.AddPushPin(48.873121, 2.374912);

    }

    public void AddIcon()
    {
        MapIcon myIcon = new MapIcon();
        myIcon.NormalizedAnchorPoint = new Point(0.5, 1.0);
        myIcon.Title = "Apartment here";
        MyMap.MapElements.Add(myIcon);

        for (int i = 0; i < pushPin.Items().Count; i++)
        {
            myIcon.Location = pushPin.MyGeopoint(i);
        }
    }
}

pushpin.cs:

internal class PushPin

{

    private ObservableCollection<Geopoint> items;

    public PushPin()
    {
        items = new ObservableCollection<Geopoint>();
    }

    public void AddPushPin(double latitude, double longitude)
    {
        items.Add(new Geopoint(new BasicGeoposition() { Latitude = latitude, Longitude = longitude }));
    }

    public Geopoint MyGeopoint(int i)
    {
        return items[i];
    }

    public ObservableCollection<Geopoint> Items()
    {
        return items;
    } 
}

I think my problem is that i constantly override the mapicon i previously created but how do i get around this? Any point in the right direction would be appreciated! Ps. I have tried with binding to an ObservableCollection but we havent been tought enough about databinding for me to figure it out.

Upvotes: 0

Views: 1254

Answers (2)

MajkeloDev
MajkeloDev

Reputation: 1661

Some time ago I had this issue and I found a solution(with MVVM), even more powerfull as it's not limiting You to adding Pushpins only.

At first Create ObservableCollection of UiElements(don't forget to implement property changed).

    private ObservableCollection<UIElement> mapElements = new ObservableCollection<UIElement>();
    public ObservableCollection<UIElement> MapElements
    {
        get
        {
            return mapElements;
        }
        set
        {
            mapElements = value;
            OnPropertyChanged("MapElements");
        }
    }

Next create a MapItemsControl inside Your map and bind its ItemsSource to collection You've just made.

<Maps:Map [...]>
    <Maps:MapItemsControl ItemsSource="{Binding MapElements}"/>
</Maps:Map>

Finally the only thing You need to do is to add Your Pushpins to MapElements Collection.

public void AddPushpinToTheMap(double longitude, double latitude)
{
    var pin = new Pushpin();
    pin.Location = new Location(latitude, longitude);
    MapElements.Add(pin);
}

That's it the pin will be visible on the map.

Upvotes: 1

Evilunclebill
Evilunclebill

Reputation: 789

The problem was more simple than I imagined. I was a dummy tbh.

    public void AddIcon()
    {

        for (int i = 0; i < pushPin.Items().Count; i++)
        {
            MapIcon myIcon = new MapIcon();
            myIcon.NormalizedAnchorPoint = new Point(0.5, 1.0);
            myIcon.Title = "Apartment here";
            MyMap.MapElements.Add(myIcon);
            myIcon.Location = pushPin.MyGeopoint(i);
        }
    }

Upvotes: 0

Related Questions