簡體   English   中英

在Windows Phone 8 Bing地圖上設置圖釘(XAML C#)

[英]Setting Up Pushpins on Windows Phone 8 Bing Maps (XAML C#)

我正在嘗試做一個簡單的示例應用程序,以便稍后在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>

實際上有更多的引腳需要添加,但我假設如果我只有一些工作,添加更多類似將是微不足道的。

你遇到的最大問題是你已經創建了第二個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中定義的引腳。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM