前回はジオコーディングについて説明しました。
今回は緯度と経度から住所を求める、逆ジオコーディングについて見ていきましょう。
※本コードを記述してみてわかりましたが、日本国内では詳しい住所は取得できないようです。
はじめにMainPage.xamlに住所を表示するためのTextBlockコントロールと、MapControlを配置します。
<StackPanel Orientation="Vertical">
<!--<StackPanel Orientation="Horizontal">
<TextBox x:Name="txtAddress" TextWrapping="Wrap" Text="" Width="381"/>
<Button x:Name="btnGetLocation" Content="現在値の取得"
HorizontalAlignment="Left" VerticalAlignment="Stretch"
Width="120" Click="btnGetLocation_Click" />
<TextBlock x:Name="txbPos" TextWrapping="Wrap" Text=""/>
</StackPanel>-->
<TextBlock x:Name="txtAddress" Width="500" HorizontalAlignment="Left" Text=""/>
<Maps:MapControl x:Name="mapControl1" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="0"
Width="500"
Height="500"
ZoomInteractionMode="GestureAndControl"
TiltInteractionMode="GestureAndControl"
MapServiceToken="取得済みのキー"
MapTapped="mapControl1_MapTapped" />
</StackPanel>
MapControlのクリックされた位置の住所を求めるようにコーディングをします。
private async void mapControl1_MapTapped(MapControl sender, MapInputEventArgs args)
{
// タップされた位置を検索
MapLocationFinderResult result =
await MapLocationFinder.FindLocationsAtAsync(args.Location);
// 検索が成功したか
if (result.Status == MapLocationFinderStatus.Success)
{
if (result.Locations.Count > 0)
{
string display =
result.Locations[0].Address.Country +
result.Locations[0].Address.District;
// 住所を表
txtAddress.Text = display;
}
}
}
result.Locations[0].Addressに取得した住所が入ってくるのですが、「日本」「東京」くらいまでの住所しか取得できませんでした。
逆ジオコーディングについてはあまり実用的でないかもしれません。
Please follow and like us:

コメント