在 Windows Phone 8 Bing 地图上设置图钉 (XAML C#)

Posted

技术标签:

【中文标题】在 Windows Phone 8 Bing 地图上设置图钉 (XAML C#)【英文标题】:Setting Up Pushpins on Windows Phone 8 Bing Maps (XAML C#) 【发布时间】:2013-03-17 18:16:54 【问题描述】:

我正在尝试为 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】:

您遇到的最大问题是您创建了第二个 Map 控件并将其显示在第一个之上。

您创建的那个没有 ZoomLevel 和 Center 集。

您还没有将带有图钉的图层添加到地图中。

查看正在发生的事情的最快方法是将构造函数更改为以下内容:

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 中定义事物并在代码中定义它们。只需在一个地方定义它们,如有必要,我可以在另一个地方引用它们。无论如何,非常感谢。

以上是关于在 Windows Phone 8 Bing 地图上设置图钉 (XAML C#)的主要内容,如果未能解决你的问题,请参考以下文章

在 Windows 8/Bing 地图应用程序中绘制坐标

windows phone 8 - 如何在地图上显示经度和纬度坐标(当前位置)

如何检查Windows Phone 8中的地图中心点是不是已更改

Windows Phone 8.1 绑定必应地图图钉

Windows phone 8.1 获取地图角落地理点

在 Windows Phone 8.0 地图上绘制路线的问题 - 0x8004231C