在Windows Phone 8 Bing Maps(XAML C#)中设置图钉

5
我正在尝试创建一个简单的示例应用程序,以后为Windows Phone 8构建。我想在我的MainPage.xaml上创建一个Bing地图,居中于点(37.227700,-80422037),并在地图上预先填充某些特定位置的推针(不是用户添加的推针,而是根据一些动态数据硬编码的标记)。当我运行代码时,它会跳转到页面并加载地图,但没有显示任何推针。此外,尽管我在xaml中设置了ZoomLevel属性,但地图仍未缩放。我对这种编程范式很陌生,所以肯定有我所缺失的东西。以下是我的xaml和c#文件:

MainPage.xaml.cs(仅显示构造函数,但出于简单起见,我没有其他方法)(您可以看到我注释掉的部分,我尝试了多种方法,但都没有起作用)

public MainPage()
    {
        InitializeComponent();
        Map myMap = new Map();
        MapLayer layer0 = new MapLayer();

        Pushpin pushpin0 = new Pushpin();
        //Pushpin pushpin0 = (Pushpin)this.FindName("pushpin0");
        //Pushpin pushpin0 = MapExtensions.GetChildren(myMap).OfType<Pushpin>().First(p => p.Name == "pushpin0");
        //if (pushpin0 == null) { pushpin0 = new Pushpin(); }
        pushpin0.GeoCoordinate = new GeoCoordinate(37.228510, -80.422860);
        MapOverlay overlay0 = new MapOverlay();
        overlay0.Content = pushpin0;
        overlay0.GeoCoordinate = new GeoCoordinate(37.228510, -80.422860);
        layer0.Add(overlay0);

        Pushpin pushpin1 = new Pushpin();
        pushpin1.GeoCoordinate = new GeoCoordinate(37.226399, -80.425271);
        MapOverlay overlay1 = new MapOverlay();
        overlay1.Content = pushpin1;
        layer0.Add(overlay1); 
        Pushpin pushpin2 = new Pushpin();
        pushpin2.GeoCoordinate = new GeoCoordinate(37.228900, -80.427450);
        MapOverlay overlay2 = new MapOverlay();
        overlay2.Content = pushpin2;
        layer0.Add(overlay2);

        ContentPanel.Children.Add(myMap);
    }

MainPage.xaml

<phone:PhoneApplicationPage
x:Class="SimpleApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
xmlns:Controls="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Maps.Toolkit;assembly=Microsoft.Phone.Controls.Toolkit">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="Simple Map Application with Pushpins" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
        <TextBlock Text="Pushpins" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Controls:Map x:Name="myMap" ZoomLevel="17" Center="37.227700, -80.422037" CartographicMode="Road">
            <toolkit:MapExtensions.Children>
                <toolkit:Pushpin x:Name="pushpin0" Content="My Position"/>
                <toolkit:Pushpin x:Name="pushpin1" Content="My Position"/>
                <toolkit:Pushpin x:Name="pushpin2" Content="My Position"/>
            </toolkit:MapExtensions.Children>
        </Controls:Map>
    </Grid>  

</Grid>

</phone:PhoneApplicationPage>

实际上还有更多的引脚需要添加,但我假设如果我只让一些工作,那么类似地添加更多将是微不足道的。

1个回答

8
你最大的问题在于你创建了第二个地图控件,并将其显示在第一个之上。
你所创建的那一个没有设置缩放级别和中心点。
你还没有将包含标记的层添加到地图上。
为了更快地了解情况,请将您的构造函数更改如下:
public MainPage()
{
    InitializeComponent();
    //Map myMap = new Map(); // You shouldn't do this as you already have a map on the page
    MapLayer layer0 = new MapLayer();

    Pushpin pushpin0 = new Pushpin();
    //Pushpin pushpin0 = (Pushpin)this.FindName("pushpin0");
    //Pushpin pushpin0 = MapExtensions.GetChildren(myMap).OfType<Pushpin>().First(p => p.Name == "pushpin0");
    //if (pushpin0 == null) { pushpin0 = new Pushpin(); }
    pushpin0.GeoCoordinate = new GeoCoordinate(37.228510, -80.422860);
    MapOverlay overlay0 = new MapOverlay();
    overlay0.Content = pushpin0;
    overlay0.GeoCoordinate = new GeoCoordinate(37.228510, -80.422860);
    layer0.Add(overlay0);

    Pushpin pushpin1 = new Pushpin();
    pushpin1.GeoCoordinate = new GeoCoordinate(37.226399, -80.425271);
    MapOverlay overlay1 = new MapOverlay();
    overlay1.Content = pushpin1;
    layer0.Add(overlay1);
    Pushpin pushpin2 = new Pushpin();
    pushpin2.GeoCoordinate = new GeoCoordinate(37.228900, -80.427450);
    MapOverlay overlay2 = new MapOverlay();
    overlay2.Content = pushpin2;
    layer0.Add(overlay2);

    // Add the layer with the pins in to the map
    myMap.Layers.Add(layer0);
    //ContentPanel.Children.Add(myMap);
}

您可以在XAML中定义的情况下删除指定的标记。


非常感谢!我知道我肯定漏掉了一些微不足道的东西,它正在添加层。我以前的代码中有这个,但不小心删除了它。我想我错过的概念是,你不必在xaml中定义东西并在代码中定义它们。只需在一个地方定义它们,如果需要,我可以在另一个地方引用它们。无论如何,非常感谢。 - eholder0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接