Muliple Pushpins in Windows Phone 8.1

2019-09-03 08:59发布

问题:

I am using MapControl to display map. I have MapServiceToken too. I have added custom pushpin using MapIcon and assigned this object to Map. It is working fine. But when i try to add multiple pushpins using same scenario and assign it to map so i am getting last pushpin on the map. Rest of other pushpins are not getting visible. Kindly tell me any simple solution to this problem. Thanks

回答1:

You can binding your pushpins on map without MapIcon.

Add in xaml

<Maps:MapControl x:Name="MapLocationsControl" MapServiceToken="add your token here">
        <Maps:MapItemsControl x:Name="mapitem">
            <Maps:MapItemsControl.ItemTemplate>
                <DataTemplate>

                        <Image Height="50" Width="50"
                               Source="{Binding image,Mode=OneWay}"
                       Maps:MapControl.Location="{Binding Geopoint}"/>

                </DataTemplate>
            </Maps:MapItemsControl.ItemTemplate>
        </Maps:MapItemsControl>
    </Maps:MapControl>

And in c# binding items in MapControl source

 mapitem.ItemsSource = items;

Where items is observable collection with MapItems class items. You can add items with Add().

ObservableCollection<MapItem> items = new ObservableCollection<MapItem>();

items.Add(new MapItem(lat,lng, "Assets/pushpinicon.png"));

MapItem Class is like this

public class MapItem {

public Geopoint Geopoint { get; set; }

public string image {get; set;}

    public MapItem(double lat , double lng , string img)
    {

        Geopoint = new Geopoint(new BasicGeoposition() { Latitude = lat, Longitude = lng });

        image=img;
     }
}