酢ろぐ!

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

Windows PhoneでBingMapsDirectionsTaskを使って地図上に経路を表示する

概要

道順の表示したい開始位置から終了位置までを指定すると、アプリケーションから経路案内モードでBing Mapsアプリケーションを起動する事が出来ます。

f:id:ch3cooh393:20150121122929j:plain

名前空間:Microsoft.Phone.Tasks

System.Object
 +--Microsoft.Phone.Tasks.BingMapsDirectionsTask

Tips

BingMapsDirectionsTaskを使って地図上に経路を表示する

BingMapsDirectionsTaskは、地図上に経路を表示するランチャーです。Windows Phone OS 7.1から使用出来るようになりました。地図上に経路を表示することが出来ます。

BingMapsDirectionsTaskクラスのインスタンスに、経路案内の開始地点であるStartプロパティと終了地点であるEndプロパティを指定して、Showメソッドを呼ぶとMapsアプリケーションを経路案内モードで起動します。

C#

using System;
using System.Device.Location;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace BingMapsDirectionsTaskTest {
    public partial class MainPage : PhoneApplicationPage {
        // コンストラクター
        public MainPage() {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e) {
            var task = new BingMapsDirectionsTask();
            try {
                // 開始地点のシアトル・タコマ国際空港を設定
                var start = new LabeledMapLocation() {
                    Label = "International Boulevard Seattle, WA",
                    Location = new GeoCoordinate(47.443937, -122.298732)
                };

                // 終了地点のマイクロソフトを設定
                var end = new LabeledMapLocation() {
                    Label = "Microsoft",
                    Location = new GeoCoordinate(47.657757, -122.142241)
                };

                task.Start = start;
                task.End = end;
                task.Show();
            } catch (InvalidOperationException ex) {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

VB.NET

Private Sub btnBingMapsDirections_Click(sender As Object, e As RoutedEventArgs)
    Dim bingMapsDirectionsTask = New BingMapsDirectionsTask()

    ' 開始地点のシアトル・タコマ国際空港を設定
    Dim start As New LabeledMapLocation()
    Dim startLocation As New GeoCoordinate(47.443937, -122.298732)
    start.Label = "International Boulevard Seattle, WA"
    start.Location = startLocation

    ' 終了地点のマイクロソフトを設定
    Dim [end] As New LabeledMapLocation()
    With [end]
        .Label = "Microsoft"
        .Location = New GeoCoordinate(47.657757, -122.142241)
    End With

    bingMapsDirectionsTask.Start = start
    bingMapsDirectionsTask.End = [end]
    bingMapsDirectionsTask.Show()
End Sub

上記のコードを実行した際のスクリーンショットです。画面の下半分にシアトル・タコマ国際空港からマイクロソフト社までの経路が表示されています。

車での経路で移動した場合の距離と大体の所要時間とが合わせて表示されているのでとても便利です。車と人のアイコンが表示されています。人を選択した場合には徒歩での経路が表示されます。

f:id:ch3cooh393:20150121122955p:plain

参照