当我尝试GeoLocator.GetPositionAsync()时,Geolocator.PositionChanged事件会升起

时间:2014-07-09 02:49:42

标签: c# xaml geolocation bing-maps

所以,在我的应用程序中使用GetPositionAsync方法获取当前位置时,我会遇到一些奇怪的行为。当我查询位置时,似乎触发了PositionChanged事件。我不确定究竟发生了什么,因为调用是异步的,所以很难调试。下面是一些说明问题的示例代码,(您需要一个Bing Maps键来测试它):

MainPage.xaml.cs中

using System;
using Bing.Maps;
using Windows.UI.Xaml.Input;
using Windows.Devices.Geolocation;
using Windows.UI.Core;
using GeoPositionExercise.Model;

namespace GeoPositionExercise
{
    public sealed partial class MainPage
    {
        private Geolocator _geolocator;
        private LocationIcon _locationIcon = new LocationIcon();

        public MainPage()
        {
            InitializeComponent();

            MyMap.Children.Add(_locationIcon);
            _geolocator = new Geolocator();

            //LoadPoly(PolyLayer, DefaultStyle, GetLocation);
            _geolocator.PositionChanged += this._geolocator_PositionChanged;

        }

        private void _geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
        {
            this.Dispatcher.RunAsync(CoreDispatcherPriority.Low, new DispatchedHandler(
                () =>
                {
                    displayPosition(this, args.Position.Coordinate);
                }));
        }

        private void displayPosition(object sender, Geocoordinate coordinate)
        {
            Location location = new Location(coordinate.Latitude, coordinate.Longitude);

            MapLayer.SetPosition(_locationIcon, location);
            MyMap.SetView(location, 15.0f);
        } 

        private async void GetLocation(object sender, TappedRoutedEventArgs e)
        {
            // This raises a PositionChanged event for some reason
            var position = await _geolocator.GetGeopositionAsync();
        }
    }
}

MainPage.xaml中

<common:PageBase xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
                 xmlns:common="using:GeoPositionExercise.Common"
                 xmlns:local="using:GeoPositionExercise"
                 xmlns:ignore="http://www.ignore.com"
                 x:Name="PageRoot"
                 x:Class="GeoPositionExercise.MainPage"
                 xmlns:m="using:Bing.Maps"
                 mc:Ignorable="d ignore"
                 DataContext="{Binding Main, Source={StaticResource Locator}}">

    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Page.Resources>

    <Page.BottomAppBar>
        <AppBar>
            <AppBarButton Label="Get Location" Tapped="GetLocation">
                <AppBarButton.Icon>
                    <FontIcon Glyph="&#xE118;" />
                </AppBarButton.Icon>
            </AppBarButton>
        </AppBar>
    </Page.BottomAppBar>

    <Grid x:Name="MainGrid" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Margin="0,0,0,53">
        <m:Map x:Name="MyMap" Credentials="YOUR BING MAP KEY HERE" >
            <m:MapLayer x:Name="PinLayer"/>
            <m:MapLayer x:Name="InfoboxLayer" Visibility="Collapsed">
                <Grid x:Name="Infobox" Width="350">
                    <Border Background="Black" Opacity="0.8" BorderBrush="White" BorderThickness="2" CornerRadius="5"/>
                    <Grid Margin="5">
                        <StackPanel HorizontalAlignment="Left" Margin="10">
                            <TextBlock x:Name="InfoboxTitle" FontSize="18" Width="290" HorizontalAlignment="Left" TextWrapping="Wrap" />
                            <WebView x:Name="InfoboxDescription" Width="330" Height="100" Margin="0,10,0,0"/>
                        </StackPanel>
                    </Grid>
                </Grid>
            </m:MapLayer>
        </m:Map>
    </Grid>
</common:PageBase>

最后是用户控件LocationIcon:

<UserControl
    x:Class="GeoPositionExercise.Model.LocationIcon"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:GeoPositionExercise.Model"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">
    <Canvas>
        <Ellipse Height="25" Width="25" Fill="#FFF0F0F0" Margin="-12.5,-12.5,0,0"></Ellipse>
        <Ellipse Height="20" Width="20" Fill="Black" Margin="-10,-10,0,0"></Ellipse>
        <Ellipse Height="10" Width="10" Fill="White" Margin="-5,-5,0,0"></Ellipse>
    </Canvas>
</UserControl>

确保在编译之前拥有正确的引用并定位X64或x82平台。您可以点击“获取位置”应用栏按钮进行测试,它始终将地图置于当前位置的中心位置。我不确定为什么会发生这种情况,因为我调用displayPosition的唯一地方是_geolocator_PostionChanged方法。

0 个答案:

没有答案