Bing Maps进阶系列二:使用GeocodeService进行地理位置检索

本文深入介绍了如何使用BingMaps的GeocodeService进行地理位置检索和反向检索,包括添加服务引用、结构分析、调用服务实现及示例代码解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  在《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提供的方法的时候会出现异常。以下为正确的客户端配置:

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> <configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<bindingname="BasicHttpBinding_IGeocodeService"maxBufferSize="2147483647"
maxReceivedMessageSize
="2147483647">
<securitymode="None">
<transport>
<extendedProtectionPolicypolicyEnforcement="Never"/>
</transport>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpointaddress="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里。界面布局大致如下:

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> <Gridx:Name="LayoutRoot"Width="500"Height="400">
<m:MapCredentialsProvider="AkzZURoD0H2Sle6Nq_DE7pm7F3xOc8S3CjDTGNWkz1EFlJJkcwDKT1KcNcmYVINU"x:Name="map"></m:Map>
<StackPanelVerticalAlignment="Top"HorizontalAlignment="Right"Background="Gray"Opacity="0.78"Orientation="Vertical"Margin="2,23,2,2">
<StackPanelOrientation="Horizontal">
<TextBlockText="地名:"Margin="0,5,0,5"></TextBlock>
<TextBoxx:Name="tbName"Width="233"></TextBox>
<Buttonx:Name="btnQuery"Content="搜索"Click="btnQuery_Click"Width="80"Height="30"></Button>
</StackPanel>
<StackPanelOrientation="Horizontal">
<TextBlockText="经度:"></TextBlock>
<TextBoxx:Name="tbLongitude"Width="110"></TextBox>
<TextBlockText="纬度:"></TextBlock>
<TextBoxx:Name="tbLatitude"Width="110"></TextBox>
<Buttonx:Name="btnQueryReverse"Content="反向搜索"Click="btnQueryReverse_Click"Width="60"Height="30"></Button>
</StackPanel>
</StackPanel>
</Grid>

        

  有了上面的界面结构,接下来看看如何实现的根据地名检索所对应的地理位置经度和纬度,这就需要调用GeocodeService所提供的Geocode()方法了。

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> privatevoidbtnQuery_Click(object sender,RoutedEventArgse)
{
//实例化GeocodeService客户端对象

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

//创建一个Geocode检索请求

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

//设置检索条件

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

//异步调用Geocode服务方法

client.GeocodeAsync(request);
}

  通过指定的GeocodeCompleted的回调函数来完成调用服务请求的响应结果,这里我将请求的结果地理位置获取到后直接将地图定位到该位置。

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> privatevoidOnGeocodeCompleted(object sender,GeocodeCompletedEventArgse)
{
if(e.Error==null
)
{
GeocodeResponseresponse
=
e.Result;
doublelatitude=response.Results[0].Locations[0
].Latitude;
doublelongitude=response.Results[0].Locations[0
].Longitude;

//显示检索地点的地理位置坐标经度和纬度

this.tbLatitude.Text= latitude.ToString();
this.tbLongitude.Text=
longitude.ToString();

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

map.SetView(newLocation(latitude,longitude),4 );
}
}

          

  

  如上图输入“china”进行检索,通过GeocodeService服务检索出了china所对应的地理位置的经度和纬度坐标,在程序中将地图定位到了该坐标的4级地图,最终效果就如上图所示。

  GeocodeService也提供了反向检索,既根据地理位置的经度和纬度进行反向检索地名,服务中的ReverseGeocode()方法便是用来实现这一功能的。

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> privatevoidbtnQueryReverse_Click(object sender,RoutedEventArgse)
{
GeocodeServiceClientclient
=new
GeocodeServiceClient();
client.ReverseGeocodeCompleted
+=newEventHandler<ReverseGeocodeCompletedEventArgs>
(OnReverseGeocodeCompleted);

ReverseGeocodeRequestrequest
=new
ReverseGeocodeRequest();
request.Credentials
=new
Credentials();
request.Credentials.ApplicationId
="AkzZURoD0H2Sle6Nq_DE7pm7F3xOc8S3CjDTGNWkz1EFlJJkcwDKT1KcNcmYVINU"
;
request.Location
=newLocation(36.5540000796318,104.055999666452
);
client.ReverseGeocodeAsync(request);
}

privatevoidOnReverseGeocodeCompleted(object
sender,ReverseGeocodeCompletedEventArgse)
{
if(e.Error==null
)
{
if(e.Result.Results.Count>0
)
{
GeocodeResponseresponse
=
e.Result;
this.tbName.Text=response.Results[0
].DisplayName;
}
else

MessageBox.Show(
"没有检索到该地理位置所对应的地点" );
}
}

  PS:貌似Bing Maps的GeocodeService有问题,难道是MS的数据有问题??通过地名检索出的地理位置坐标反向检索回去居然没有数据返回。

  其实说简单点GeocodeService所提供的两个方法就是一个实现根据地名检索对应的地理位置经度和纬度坐标,一个实现根据地理位置的经度和纬度坐标反向检索地名。这在外网GIS应用中是非常有用的,企业内网的业务支撑系统这一功能到不是很有用,通常企业业务系统的业务扩展开发中应用到地图最多的就是地图的呈现,比如将一些网络组织图以GIS的形式呈现出来,让人看着比较直观、清晰,其他扩展全的根据业务需求开发。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值