[英]Adding multiple pushpins in bing map using WPF control in winform application
[英]Adding multiple pushpins to bing map
我正在嘗試將多個圖釘添加到Windows 10應用程序中的bing映射中。 我面臨的麻煩是,最后添加的經緯度只是添加的時間長。 讓我發布我的代碼:
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,但是如何解決呢? 朝正確方向的任何觀點將不勝感激! 附言 我已經嘗試過綁定到ObservableCollection
但是我們在數據綁定方面還不夠強硬,我無法弄清楚。
不久前,我遇到了這個問題,我找到了一個解決方案(使用MVVM),功能更強大,因為它不僅僅限制您添加圖釘。
首先創建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>
最后,您唯一需要做的就是將圖釘添加到MapElements集合中。
public void AddPushpinToTheMap(double longitude, double latitude)
{
var pin = new Pushpin();
pin.Location = new Location(latitude, longitude);
MapElements.Add(pin);
}
就是這樣,圖釘將在地圖上可見。
這個問題比我想象的簡單。 我是個傻瓜。
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.