如何以编程方式添加PushPin,是否可以使其具有自定义图像?

时间:2013-01-10 20:57:16

标签: c# maps windows-phone-8

我正在尝试创建一个地图应用程序,但我找到的示例描述了我的myMap.Children对象没有的myMap列表: - (

我已经创建了一张地图,非常简单:

<maps:Map Visibility="Collapsed" Name="MyMap" Height="670" Width="400" ZoomLevel="10" Pitch="0" CartographicMode="Hybrid" Margin="30,0" />

那么我怎样才能在C#中添加PushPins,并且这些是否有来自Assets的图像?

1 个答案:

答案 0 :(得分:9)

请参阅“Adding Graphics to a Map control”上的诺基亚地图教程,或参阅MSDN的“How to add UIElements to a Map control in Windows Phone 8”。

主要是在它上面加上你自己的MapLayer和多个MapOverlay:

private void DrawMapMarkers()
{
    MyMap.Layers.Clear();
    MapLayer mapLayer = new MapLayer();

    // Draw marker for current position
    if (MyCoordinate != null)
    {
        DrawAccuracyRadius(mapLayer);
        DrawMapMarker(MyCoordinate, Colors.Red, mapLayer);
    }

    ...

    MyMap.Layers.Add(mapLayer);
}

private void DrawMapMarker(GeoCoordinate coordinate, Color color, MapLayer mapLayer)
{
    // Create a map marker
    Polygon polygon = new Polygon();
    polygon.Points.Add(new Point(0, 0));
    polygon.Points.Add(new Point(0, 75));
    polygon.Points.Add(new Point(25, 0));
    polygon.Fill = new SolidColorBrush(color);

    // Enable marker to be tapped for location information
    polygon.Tag = new GeoCoordinate(coordinate.Latitude, coordinate.Longitude);
    polygon.MouseLeftButtonUp += new MouseButtonEventHandler(Marker_Click);

    // Create a MapOverlay and add marker
    MapOverlay overlay = new MapOverlay();
    overlay.Content = polygon;
    overlay.GeoCoordinate = new GeoCoordinate(coordinate.Latitude, coordinate.Longitude);
    overlay.PositionOrigin = new Point(0.0, 1.0);
    mapLayer.Add(overlay);
}

为了对新的WP8诺基亚地图控件进行数据绑定,请使用新Windows Phone Toolkit中的MapExtensions。例如,这里使用MapExtensions how to create a PushPin in a specific GeoCoordinate

<maps:Map x:Name="Map" Grid.Row="1" Hold="OnMapHold">
    <maptk:MapExtensions.Children>
        <maptk:Pushpin x:Name="RouteDirectionsPushPin" Visibility="Collapsed"/>
        <maptk:MapItemsControl Name="StoresMapItemsControl">
            <maptk:MapItemsControl.ItemTemplate>
                <DataTemplate>
                    <maptk:Pushpin GeoCoordinate="{Binding GeoCoordinate}" Visibility="{Binding Visibility}" Content="{Binding Address}"/>
                </DataTemplate>
            </maptk:MapItemsControl.ItemTemplate>
        </maptk:MapItemsControl>
        <maptk:UserLocationMarker x:Name="UserLocationMarker" Visibility="Collapsed"/>
    </maptk:MapExtensions.Children>
</maps:Map>