The thing is that I cannot see all 20.000 MapIcons at once and I'd like to increase the number of MapIcons even more.
I have a System.Collections.ObjectModel.ObservableCollection<House>
prop called Houses
in my ViewModel HousesViewModel
public MainPage()
{
this.InitializeComponent();
HousesVM.Houses.CollectionChanged += Houses_CollectionChangedAsync;
GetInitialPins();
}
My GetInitialPins()
had a foreach to add/fill the HousesVm.Houses but that seemed inefficient so I rewrote it like this:
List<House> houses = JsonConvert.DeserializeObject<List<House>>(response);
HousesVM.Houses = new System.Collections.ObjectModel.ObservableCollection<House>(houses);
The downside is that this does not fire the CollectionChanged
event in which I added the MapIcons.
private async void Houses_CollectionChangedAsync(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>
{
foreach (House item in e.NewItems)
{
MapIcon myPOI = new MapIcon
{
Location = new Geopoint(new BasicGeoposition() { Latitude = item.Latitude, Longitude = item.Longitude }),
NormalizedAnchorPoint = normalizedAnchorPoint,
Title = item.Name,
CollisionBehaviorDesired = MapElementCollisionBehavior.RemainVisible,
ZIndex = 0
};
await mappie.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
{
mappie.MapElements.Add(myPOI);
});
}
});
}
I have also tried to move away from MapIcon
and just add Ellipse
to the children of the mapcontrol. That gave me an out of memory exception. Also tried to bind in XAML:
<map:MapControl x:Name="mappie"
MapServiceToken="mysecrettoken"
Grid.Row="1">
<map:MapItemsControl ItemsSource="{Binding Houses}">
<map:MapItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock map:MapControl.Location="{Binding Location}" Text="{Binding Code}" map:MapControl.NormalizedAnchorPoint="0.5,0.5" FontSize="20" Margin="5"/>
</DataTemplate>
</map:MapItemsControl.ItemTemplate>
</map:MapItemsControl>
Also gave an out of memory exception... Have also seen that the js version of bing maps has a cluster/grouping feature. which is not there in uwp. And also seen in the docs that with large quantity I should consider building a tile service and reference custom created tiles... That seems an overkill for my situation.
Clustering is the recommended method of dealing with a very large number of points since as you noted you really can't see all of them at the same time anyway. There is a clustering example in the UWP maps sample here: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MapControl