GeocodeService服务用来检索地理位置
效果如图
一、添加服务引用
GeocodeService服务也是WCF服务,地址是http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc
二、实例
1. public class ChinaTileSource
{
/// <summary>
/// 加载中国地图系统
/// http://r2.tiles.ditu.live.com/tiles/r{quadkey}.png?g=41中国地图系统
/// </summary>
/// <returns>TileSource</returns>
public TileSource GetChinaTileSource()
{
UriBuilder tileSourceUri = new UriBuilder("http://r2.tiles.ditu.live.com/tiles/r{quadkey}.png?g=41");
MapTileLayer tileLayer = new MapTileLayer();
LocationRectTileSource tileSource = new LocationRectTileSource(tileSourceUri.Uri.ToString()
, new LocationRect(new Location(60, 60), new Location(13, 140)), new Range<double>(1, 16));
return tileSource;
}
}
2.前台
<UserControl x:Class="SlBindMapDemo.GeocodeServiceDemo"
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:map="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Width="500" Height="400">
<map:Map CredentialsProvider="AkGGA_JlwP7XGV8JxIPb8oEWxrInlLMGKpCe7QM4QB5cg4UGNCqUyjqVfC0B2-XC" x:Name="myMap"></map:Map>
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Right" Background="Gray" Opacity="0.78" Orientation="Vertical" Margin="2,23,2,2">
<StackPanel Orientation="Horizontal">
<TextBlock Text="地名:" Margin="0,5,0,5"></TextBlock>
<TextBox x:Name="tbName" Width="233"></TextBox>
<Button x:Name="btnQuery" Content="搜索" Click="btnQuery_Click" Width="80" Height="30"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="经度:"></TextBlock>
<TextBox x:Name="tbLongitude" Width="110"></TextBox>
<TextBlock Text="纬度:"></TextBlock>
<TextBox x:Name="tbLatitude" Width="110"></TextBox>
<Button x:Name="btnQueryReverse" Content="反向搜索" Click="btnQueryReverse_Click" Width="60" Height="30"></Button>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
3.后台
public partial class GeocodeServiceDemo : UserControl
{
const string BingMapID = "AkGGA_JlwP7XGV8JxIPb8oEWxrInlLMGKpCe7QM4QB5cg4UGNCqUyjqVfC0B2-XC";
public GeocodeServiceDemo()
{
InitializeComponent();
MapTileLayer tileLayer = new MapTileLayer();
ChinaTileSource gts = new ChinaTileSource();
tileLayer.TileSources.Add(gts.GetChinaTileSource());
myMap.Children.Add(tileLayer);
}
/// <summary>
/// 检索
///根据国家或地名检索
/// GeocodeRequest用来处理正向检索请求
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQuery_Click(object sender, RoutedEventArgs e)
{
GeocodeServiceClient client = new GeocodeServiceClient();
client.GeocodeCompleted += new EventHandler<GeocodeCompletedEventArgs>(OnGeocodeCompleted);
//创建请求
GeocodeRequest request = new GeocodeRequest();
request.Credentials = new Credentials();
//BingMap ID
request.Credentials.ApplicationId = BingMapID;
//检索条件
request.Query = this.tbName.Text.Trim();
//进行检索
client.GeocodeAsync(request);
}
private void OnGeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
{
if (e.Error == null)
{
//获取结果
GeocodeResponse response = e.Result;
double latitude = response.Results[0].Locations[0].Latitude;
double longitude = response.Results[0].Locations[0].Longitude;
this.tbLatitude.Text = latitude.ToString();
this.tbLongitude.Text = longitude.ToString();
//根据经纬度和级别设置视图,并显示该位置
myMap.SetView(new Location(latitude, longitude), 4);
}
}
/// <summary>
/// 反向检索
///根据经纬度检索
/// ReverseGeocodeRequest用来处理反向检索请求
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQueryReverse_Click(object sender, RoutedEventArgs e)
{
GeocodeServiceClient client = new GeocodeServiceClient();
client.ReverseGeocodeCompleted += new EventHandler<ReverseGeocodeCompletedEventArgs>(OnReverseGeocodeCompleted);
ReverseGeocodeRequest request = new ReverseGeocodeRequest();
request.Credentials = new Credentials();
request.Credentials.ApplicationId = BingMapID;
request.Location = new Location(int.Parse(this.tbLongitude.Text), int.Parse(this.tbLatitude.Text));
client.ReverseGeocodeAsync(request);
}
private void OnReverseGeocodeCompleted(object sender, ReverseGeocodeCompletedEventArgs e)
{
if (e.Error == null)
{
if (e.Result.Results.Count > 0)
{
GeocodeResponse response = e.Result;
this.tbName.Text = response.Results[0].DisplayName;
double latitude = response.Results[0].Locations[0].Latitude;
double longitude = response.Results[0].Locations[0].Longitude;
//根据经纬度和级别设置视图,并显示该位置,这一点很重要啊。
myMap.SetView(new Location(latitude, longitude), 4);
}
else
MessageBox.Show("没有检索到该地址");
}
}
}