在《 Bing Maps进阶系列一:初识Bing Maps地图服务》里已经对GeocodeService的功能进行了简单的描述说明,本篇将会详细的介绍如何使用GeocodeService进行地理位置检索和反向检索的实现。
 
一、添加GeocodeService的Web服务引用
  地理编码服务(GeocodeService)是以WCF技术发布的一个Web服务,地图编码服务提供了以一个有效的物理地址在地图上匹配其对应的地图地址(既地理经度和纬度坐标)和以地理经度和纬度坐标进行反向匹配物理地址路径的功能。要使用该服务需添加该服务( http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc)的Web服务引用,如下图:
        
  
  从上图可以清晰的看出,该服务提供了两个方法,一个是根据地名检索所对应的地理经度和纬度坐标的,另一个方法实现反向检索地名的功能。通过添加Web服务引用向导IDE工具会为我们自动生成Web服务引用的代理对象等,这些知识点数据Web Service或WCF相关的,这里不详细介绍。
 
 二、GeocodeService的结构分析
  我们可以通过查看GeocodeService的客户端代理对象类图知道,客户端的调用只支持异步调用方式,如下图:
        
 
  地点检索和反向地理坐标检索都只支持客户端异步调用,添加Web服务引用后可通过生成的WCF客户端配置文件查看到客户端调用WCF的相关配置,这里需要注意一点,自动生成的配置有两个服务端点的地址配置,需要人为将其中一个"custom"配置删除,或者在调用GeocodeService提供的方法的时候会出现异常。以下为正确的客户端配置:
<configuration>
    
<system.serviceModel>
        
<bindings>
            
<basicHttpBinding>
                
<binding name="BasicHttpBinding_IGeocodeService" maxBufferSize="2147483647"
                    maxReceivedMessageSize
="2147483647">
                    
<security mode="None">
                        
<transport>
                            
<extendedProtectionPolicy policyEnforcement="Never" />
                        
transport>
                    
security>
                
binding>
            
basicHttpBinding>
        
bindings>
        
<client>
            
<endpoint address="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc"
                binding
="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IGeocodeService"
                contract
="GeocodeService.IGeocodeService" name="BasicHttpBinding_IGeocodeService" />
        
client>
    
system.serviceModel>
configuration>
 
 
三、调用GeocodeService服务
  首先将界面布局设计下,通过一个TextBox来输入地点名称,一个Button来发起服务调用请求并将检索过后的结果(地名对应的地理位置经度和纬度)显示在另外两个TextBox里。界面布局大致如下:
 
代码
<Grid x:Name="LayoutRoot" Width="500" Height="400">
    
<m:Map CredentialsProvider="AkzZURoD0H2Sle6Nq_DE7pm7F3xOc8S3CjDTGNWkz1EFlJJkcwDKT1KcNcmYVINU" x:Name="map">m: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>
 
        
 
  有了上面的界面结构,接下来看看如何实现的根据地名检索所对应的地理位置经度和纬度,这就需要调用GeocodeService所提供的Geocode()方法了。
 
代码
private void btnQuery_Click(object  sender, RoutedEventArgs e)
{
    
//实例化GeocodeService客户端对象

    GeocodeServiceClient client = new  GeocodeServiceClient();
    client.GeocodeCompleted 
+= new EventHandler<GeocodeCompletedEventArgs>
(OnGeocodeCompleted);

    
//创建一个Geocode检索请求

    GeocodeRequest request = new  GeocodeRequest();
    request.Credentials 
= new
 Credentials();
    request.Credentials.ApplicationId 
= "AkzZURoD0H2Sle6Nq_DE7pm7F3xOc8S3CjDTGNWkz1EFlJJkcwDKT1KcNcmYVINU"
;

    
//设置检索条件

    request.Query = this .tbName.Text.Trim();

    
//异步调用Geocode服务方法

    client.GeocodeAsync(request);
}
 
 
  通过指定的GeocodeCompleted的回调函数来完成调用服务请求的响应结果,这里我将请求的结果地理位置获取到后直接将地图定位到该位置。
 
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();

        
//将地图定位到该地理位置并设置地图缩放级别到4级

        map.SetView(new Location(latitude, longitude), 4 );
    }
}
 
 
           
  
  如上图输入“china”进行检索,通过GeocodeService服务检索出了china所对应的地理位置的经度和纬度坐标,在程序中将地图定位到了该坐标的4级地图,最终效果就如上图所示。
 
  GeocodeService也提供了反向检索,既根据地理位置的经度和纬度进行反向检索地名,服务中的ReverseGeocode()方法便是用来实现这一功能的。
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 
= "AkzZURoD0H2Sle6Nq_DE7pm7F3xOc8S3CjDTGNWkz1EFlJJkcwDKT1KcNcmYVINU"
;
    request.Location 
= new Location(36.5540000796318,104.055999666452
);
    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;
        }
        
else

            MessageBox.Show(
"没有检索到该地理位置所对应的地点" );
    }
}
 
   PS:貌似Bing Maps的GeocodeService有问题,难道是MS的数据有问题??通过地名检索出的地理位置坐标反向检索回去居然没有数据返回。 
 
  其实说简单点GeocodeService所提供的两个方法就是一个实现根据地名检索对应的地理位置经度和纬度坐标,一个实现根据地理位置的经度和纬度坐标反向检索地名。这在外网GIS应用中是非常有用的,企业内网的业务支撑系统这一功能到不是很有用,通常企业业务系统的业务扩展开发中应用到地图最多的就是地图的呈现,比如将一些网络组织图以GIS的形式呈现出来,让人看着比较直观、清晰,其他扩展全的根据业务需求开发。
 
  想了解更多关于Bing Maps地图服务的知识,请查询:
  MSDN: http://msdn.microsoft.com/en-us/library/cc980922.aspx 
  Bing Maps开发站: http://www.microsoft.com/maps/developers/
  Bing Maps开发SDK: http://msdn.microsoft.com/en-us/library/dd877180.aspx