问题
I am struggling to add a collection of latitude & longitude as Pushpins on Bing Map, in a Windows 8 app using XAML & C#.
Adding Pushpins one by one using an event handler such as a right tap on the map works fine.
Here is the XAML:
<bm:Map x:Name="myMap" Grid.Row="0" MapType="Road" ZoomLevel="14" Credentials="{StaticResource BingMapAPIKey}" ShowTraffic="False" Tapped="map_Tapped" >
<bm:Map.Center>
<bm:Location Latitude="-37.812751" Longitude="144.968204" />
</bm:Map.Center>
</bm:Map>
And here is the handler:
private void map_Tapped(object sender, TappedRoutedEventArgs e)
{
// Retrieves the click location
var pos = e.GetPosition(myMap);
Bing.Maps.Location location;
if (myMap.TryPixelToLocation(pos, out location))
{
// Place Pushpin on the Map
Pushpin pushpin = new Pushpin();
pushpin.RightTapped += pushpin_RightTapped;
MapLayer.SetPosition(pushpin, location);
myMap.Children.Add(pushpin);
// Center the map on the clicked location
myMap.SetView(location);
}
}
All of the above works. If I tap the map, a new Pushpin is added.
Now, when I try to add pushpin when initializing the page by iterating on a list, only the last Pushpin of the list is displayed on the map, as if each new Pushpin was overwriting the previous one. Here is the code I use:
protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
...
// The Venue class is a custom class, the Latitude & Logitude are of type Double
foreach (Venue venue _venues)
{
Bing.Maps.Location location = new Location(venue.Latitude, venue.Longitude);
// Place Pushpin on the Map
Pushpin pushpin = new Pushpin();
pushpin.RightTapped += pushpin_RightTapped;
MapLayer.SetPosition(pushpin, location);
myMap.Children.Add(pushpin);
// Center the map on the clicked location
myMap.SetView(location);
}
...
}
As you can see, I use the same code, but at the end of the LoadState method, the map only displays the last location. In case you are wondering, the foreach loop is fully executed for each location.
Is there any way to get this to work, or even better, to bind directly the map children to a ObservableCollection of Pushpin objects ? I feel like I am so close but I can't figure out what I missed.
Please help !
回答1:
You should keep the different locations in an array (or a list or more efficient LocationCollection) and call the SetView method only after you've looped throught your elements.
LocationCollection locationCollection = new LocationCollection ();
// The Venue class is a custom class, the Latitude & Logitude are of type Double
foreach (Venue venue _venues)
{
Bing.Maps.Location location = new Location(venue.Latitude, venue.Longitude);
// Place Pushpin on the Map
Pushpin pushpin = new Pushpin();
pushpin.RightTapped += pushpin_RightTapped;
MapLayer.SetPosition(pushpin, location);
myMap.Children.Add(pushpin);
locationCollection.Append(location);
}
myMap.SetView(new LocationRect(locationCollection));
来源:https://stackoverflow.com/questions/14936300/windows-8-app-xaml-c-set-multiple-pushpins-to-bing-map-in-one-method