How can I add programmatically add a PushPin, and

2019-02-04 03:35发布

问题:

I'm trying to create a map application but the examples I find describes a myMap.Children list that my myMap object does not have :-(

I've created a map, pretty much straight forward:

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

So how can I in C# add PushPins, and could these have a image from Assets?

回答1:

See Nokia's Maps tutorial on "Adding Graphics to a Map control" or see MSDN's "How to add UIElements to a Map control in Windows Phone 8".

It's mostly around adding you own MapLayer with multiple MapOverlay on top of it:

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);
}

In order to databind the new WP8 Nokia Map control, use MapExtensions from the new Windows Phone Toolkit. For example here's how to create a PushPin in a specific GeoCoordinate using MapExtensions.

<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>