将多个图钉添加到bing map

时间:2015-11-06 10:13:51

标签: c# bing-maps pushpin

我正在尝试将多个图钉添加到Windows 10应用中的bing地图中。我遇到的麻烦是它只是最后添加的lat,很长时间才会被添加。让我发布我的代码:

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

我认为我的问题是我经常覆盖我之前创建的mapicon但是我如何解决这个问题呢? 正确方向的任何一点将不胜感激! PS。我已尝试绑定到ObservableCollection,但我们还没有充分考虑数据绑定让我弄明白。

2 个答案:

答案 0 :(得分:1)

前段时间我遇到了这个问题,我发现了一个解决方案(使用MVVM),因为它不仅仅限制你添加Pushpins。

首先创建UiElements的ObservableCollection(不要忘记实现属性更改)。

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

接下来在地图中创建一个MapItemsControl,并将其ItemsSource绑定到您刚刚制作的集合。

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

最后,您唯一需要做的就是将您的Pushpins添加到MapElements Collection。

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

就是它会在地图上显示引脚。

答案 1 :(得分:0)

问题比我想象的更简单。我是个假人。

    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);
        }
    }
相关问题