如何在WP8中添加多个图钉来映射

时间:2015-02-05 11:53:28

标签: c# xaml windows-phone-8 dictionary

我想创建一个定位器,我有一个包含500多个元素的json文档。每个元素看起来像:

{
    "lat": "52.500141",
    "longi": "13.312320",
    "_name": "Restaurant Berlin"
}

现在我在我的代码中执行此操作:Link to Tutorial

带有地图元素的我的xaml:

<maps:Map x:Name="mymap" ColorMode="Light" Center="50.9727,010.2145" ZoomLevel="6" HorizontalAlignment="Left" Margin="-13,0,-13,-72" Width="482" CartographicMode="Road">
            <toolkit:MapExtensions.Children>
                <toolkit:MapItemsControl Name="RestaurantItems">
                    <toolkit:MapItemsControl.ItemTemplate>
                        <DataTemplate>
                            <toolkit:Pushpin PositionOrigin="0,0" GeoCoordinate="{Binding Coordinate}">
                                <toolkit:Pushpin.Template>
                                    <ControlTemplate TargetType="toolkit:Pushpin">
                                        <StackPanel>
                                            <Image Source="/Resources/marker.png" Stretch="Uniform" Width="150" Height="100" HorizontalAlignment="Center"/>
                                        </StackPanel>
                                    </ControlTemplate>
                                </toolkit:Pushpin.Template>
                            </toolkit:Pushpin>
                        </DataTemplate>
                    </toolkit:MapItemsControl.ItemTemplate>
                </toolkit:MapItemsControl>
            </toolkit:MapExtensions.Children>
</maps:Map>

在我读完json文件后,它看起来像这样:

       ObservableCollection<Restaurantmarker> restaurants = new ObservableCollection<Restaurantmarker>();

        for(int k = 0; k < JsonObjects.Count; k++)
        {
            double a = double.Parse(JsonObjects[k].lat, CultureInfo.InvariantCulture);
            double b = double.Parse(JsonObjects[k].longi, CultureInfo.InvariantCulture);

            new Restaurantmarker()
            { 
                _name = JsonObjects[k]._name, 
                Coordinate = new GeoCoordinate (a,b) 
            };
        };

        ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(mymap);
        var obj = children.FirstOrDefault(x => x.GetType() == typeof(MapItemsControl)) as MapItemsControl;

        obj.ItemsSource = restaurants;

我的Observable Collection课程:

public class Restaurantmarker
{
    public GeoCoordinate Coordinate { get; set; }

    public string _name { get; set; }
}

用较少的,只有20个元素才能很好地工作,但是超过500个它不起作用。有谁能够帮我?

1 个答案:

答案 0 :(得分:0)

如果你想在地图上设置多个图钉,首先你需要有他们的纬度和经度信息,之后你可以这样做:

    private void setPinsOnTheMap()
    {
        MapLayer layer = new MapLayer();
        for (int i = 0; i < elementsList.Count; i++)
        {
            Pushpin pin = new Pushpin();
            pin.GeoCoordinate = elementsList.ElementAt(i).geoLocation;

            MapOverlay overlay = new MapOverlay();
            overlay.GeoCoordinate = elementsList.ElementAt(i).geoLocation;
            overlay.Content = pin;

            layer.Add(overlay);
        }
        map.Layers.Add(layer);
    }