酢ろぐ!

カレーが嫌いなスマートフォンアプリプログラマのブログ。

Windows Phoneで位置情報を取得してMapコントロールにプロットする

ロケーションサービスからの位置情報の変更を受け取り、マップ上に紫色のピンを立てていきます。

f:id:ch3cooh393:20141113134907p:plain

ContentPanelにMapコントロールを配置しただけのXAMLです。

<phone:PhoneApplicationPage
    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"
    xmlns:Microsoft_Phone_Controls_Maps="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps" 
    x:Class="GeoCoordinateWatcherTest.MainPage"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot は、すべてのページ コンテンツが配置されるルート グリッドです-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel は、アプリケーション名とページ タイトルを格納します-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="SOFTBUILD" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="geocoordinate watcher test" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontSize="64" />
        </StackPanel>

        <!--ContentPanel - 追加コンテンツをここに入力します-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <StackPanel Orientation="Vertical">
                <TextBlock x:Name="labelStatus" TextWrapping="Wrap" Text="Status" Style="{StaticResource PhoneTextLargeStyle}" Margin="0,6" Foreground="{StaticResource PhoneAccentBrush}"/>
                <StackPanel Height="44" Orientation="Horizontal" Margin="0,6">
                    <TextBlock Margin="0" Text="latitude" Style="{StaticResource PhoneTextLargeStyle}" Width="160" />
                    <TextBlock x:Name="labelLatitude" Style="{StaticResource PhoneTextLargeStyle}" Text="0.000000" Foreground="{StaticResource PhoneAccentBrush}" />
                </StackPanel>
                <StackPanel Height="44" Orientation="Horizontal" Margin="0,6">
                    <TextBlock Margin="0" Text="longitude" Style="{StaticResource PhoneTextLargeStyle}" Width="160" />
                    <TextBlock x:Name="labelLongitude" Text="0.000000" Style="{StaticResource PhoneTextLargeStyle}" Foreground="{StaticResource PhoneAccentBrush}" />
                </StackPanel>
                <Microsoft_Phone_Controls_Maps:Map x:Name="map1" d:LayoutOverrides="Width" Margin="0,6,0,0" ZoomLevel="10" Height="444"/>
            </StackPanel>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

位置情報を取得する」では、取得した位置情報をTextBlockコントロールに表示させただけですが、ここでは位置情報からピンオブジェクトを生成してMapコントロールに設定します。

// 位置情報が変更された場合に呼ばれるイベントハンドラ
void watcher_PositionChanged(object sender, 
    GeoPositionChangedEventArgs<GeoCoordinate> e)
{
    // Mapコントロールにピンを追加したいのでUIスレッド上で実行させる
    Deployment.Current.Dispatcher.BeginInvoke(() => {

        double latitude = e.Position.Location.Latitude;
        double longitude = e.Position.Location.Longitude;

        // データを取得した位置を中心にする
        map1.Center = new GeoCoordinate(latitude, longitude);

        // 現在地に紫色のピンを立てる
        var pin = new Pushpin() {
            Background = new SolidColorBrush(Colors.Purple),
            Location = new GeoCoordinate(latitude, longitude)
        };
        map1.Children.Add(pin);
    });
}

上記のコードを実行するとPositionChangedイベントハンドラに通知された位置情報を元にピンがプロットされているのが分かります。

f:id:ch3cooh393:20141113134702p:plain

テストの際には、Windows Phoneシミュレータに付属するテストツールを利用すると仮想的な位置情報を取得することができます。デバッグの度に自転車のかごに端末を入れて外出する必要がなくなります。

関連記事