使用 Bing Maps Web 服务进行 WPF 应用程序的示例





3.00/5 (5投票s)
这是一个示例 WPF 应用程序,它使用了 Bing Maps Web 服务中的 GeocodeService、SearchService、ImageryService 和 RouteService。
引言
本文演示了一个示例 WPF 应用程序,该应用程序可以消耗 Bing Maps Web 服务。如果您的业务需求表明需要构建桌面应用程序,那么这将非常有用。本文将演示如何在桌面应用程序中使用 Bing Maps Web 服务的 Geocode、Imagery、Route 和 Search 服务,并使用 Windows Presentation Foundation (WPF) 和 C#。
背景
是否曾想将 Bing Maps 集成到您的 Windows 应用程序中?这里是解决方案。您只需要 Visual Studio 2008、.NET Framework 3.0 和 Bing Maps Platform Developer Account 凭证。您可以在 Bing Maps Account Center 注册一个免费开发者账户。Bing Maps Web 服务要求您拥有 Bing Maps 密钥才能发出请求。当您登录到 Bing Maps Account Center 时,可以创建一个密钥。有关创建密钥的更多信息,请参阅 Accessing the Bing Maps Web Services。
Using the Code
一旦您拥有了 Bing Maps 账户和密钥,您就可以开始创建引用 Bing Maps Web 服务的代理类了。为此,您需要设置服务引用,它们提供了地理编码、地图和搜索功能。这些被添加为 Windows Communication Foundation (WCF) 服务,Visual Studio 会为项目构建代理文件。
代码很简单。在调用这些服务的任何方法之前,您必须做的第一件事是设置从您的 Bing Maps 账户获得的密钥。完成后,您创建一个想要使用的服务实例,并尝试识别您想要调用的方法。通常,方法需要一个类型的对象作为其参数,因此花一些时间来理解参数以及需要设置哪些属性。
您会注意到应用程序有 6 个按钮。每个按钮的作用如下:
1. GeoCode
此按钮将邮政地址转换为地理编码(经度和纬度)。此功能基于 Geocode Service。Geocode Service 提供了地理编码地址和反向地理编码位置的方法。
以下是解释如何获取指定地址的地理编码的代码。它非常简单。首先,设置您从 Bing Maps 账户获得的密钥作为 GeocodeRequest
的凭证。接下来,设置过滤器。完成后,创建 GeoServiceClient
的实例并调用其 GeoCode
方法。
private GeocodeResponse GeocodeAddressGeocodeResponse(string address)
{
GeocodeRequest geocodeRequest = new GeocodeRequest()
// Set the credentials using a valid Bing Maps key
geocodeRequest.Credentials = new GeocodeService.Credentials();
geocodeRequest.Credentials.ApplicationId = key;
// Set the full address query
geocodeRequest.Query = address;
// Set the options to only return high confidence results
ConfidenceFilter[] filters = new ConfidenceFilter[1];
filters[0] = new ConfidenceFilter();
filters[0].MinimumConfidence = GeocodeService.Confidence.High;
// Add the filters to the options
GeocodeOptions geocodeOptions = new GeocodeOptions();
geocodeOptions.Filters = filters;
geocodeRequest.Options = geocodeOptions;
// Make the geocode request
GeocodeServiceClient geocodeService = new GeocodeServiceClient();
GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);
return geocodeResponse;
}
这是截图

2. Reverse GeoCode
顾名思义,您可以将地理编码转换为可读的邮政地址。它与上面解释的 Get GeoCode
方法非常相似。
3. Search
此方法获取查询的“什么”和“在哪里”部分。“什么”是指加油站、理发店等,“在哪里”是指位置。它返回一个搜索结果列表。应用程序显示名称和距离,但您可以对收到的数据做更多的事情。
以下是获取指定位置附近搜索结果的代码片段。
private string SearchKeywordLocation(string keywordLocation)
{
String results = "";
SearchRequest searchRequest = new SearchRequest();
// Set the credentials using a valid Bing Maps key
searchRequest.Credentials = new SearchService.Credentials();
searchRequest.Credentials.ApplicationId = key;
//Create the search query
StructuredSearchQuery ssQuery = new StructuredSearchQuery();
string[] parts = keywordLocation.Split(';');
ssQuery.Keyword = parts[0];
ssQuery.Location = parts[1];
searchRequest.StructuredQuery = ssQuery;
//Define options on the search
searchRequest.SearchOptions = new SearchOptions();
searchRequest.SearchOptions.Filters =
new FilterExpression()
{
PropertyId = 3,
CompareOperator = CompareOperator.GreaterThanOrEquals,
FilterValue = 8.16
};
//Make the search request
SearchServiceClient searchService = new SearchServiceClient();
SearchResponse searchResponse = searchService.Search(searchRequest);
//Parse and format results
if (searchResponse.ResultSets[0].Results.Length > 0)
{
StringBuilder resultList = new StringBuilder("");
for (int i = 0; i < searchResponse.ResultSets[0].Results.Length; i++)
{
resultList.Append(String.Format("{0}. {1},
[Dist]{2}, [Loc]{3}, [Id]{4}\n", i + 1,
searchResponse.ResultSets[0].Results[i].Name,
searchResponse.ResultSets[0].Results[i].Distance,
searchResponse.ResultSets[0].Results[i].LocationData,
searchResponse.ResultSets[0].Results[i].Id));
}
results = resultList.ToString();
}
else
results = "No results found";
return results;
}
这是快照
4. MapIt (L&L)
此方法接收地理编码并显示该区域的地图。有关更多详细信息,请参阅下面的第 5 点。
5. MapIt
此方法接收类似英文的地址并显示地图。您还会看到一个滚动条,您可以使用它来设置缩放级别。
这是代码片段。它很简单。您创建 Imagery Service 的实例并调用其 GetMapUri
方法。此方法将接受 MapUriRequests
对象。这个对象是您获取经度、纬度和缩放级别的对象。
private string GetImagery(string locationString, int zoom)
{
MapUriRequest mapUriRequest = new MapUriRequest();
// Set credentials using a valid Bing Maps key
mapUriRequest.Credentials = new ImageryService.Credentials();
mapUriRequest.Credentials.ApplicationId = key;
// Set the location of the requested image
mapUriRequest.Center = new ImageryService.Location();
string[] digits = locationString.Split(',');
mapUriRequest.Center.Latitude = double.Parse(digits[0].Trim());
mapUriRequest.Center.Longitude = double.Parse(digits[1].Trim());
// Set the map style and zoom level
MapUriOptions mapUriOptions = new MapUriOptions();
mapUriOptions.Style = MapStyle.AerialWithLabels;
mapUriOptions.ZoomLevel = zoom;
// Set the size of the requested image in pixels
mapUriOptions.ImageSize = new ImageryService.SizeOfint();
mapUriOptions.ImageSize.Height = 200;
mapUriOptions.ImageSize.Width = 300;
mapUriRequest.Options = mapUriOptions;
//Make the request and return the URI
ImageryServiceClient imageryService = new ImageryServiceClient();
MapUriResponse mapUriResponse = imageryService.GetMapUri(mapUriRequest);
return mapUriResponse.Uri;
}
这是快照

6. Route
此方法接受两个地址并返回这两个端点之间的路线。Route Service 可以计算航点之间的路线。与所有其他服务一样,您需要先创建一个请求对象。RouteRequest
对象包括要计算路线的航点数组以及如何计算路线的选项。结果对象包含一系列分步说明。
为了获得路线,您需要使用 RouteService
。此服务需要 WayPoint
的实例,您可以在其中设置起点和终点的坐标。填充完 WayPoint
对象后,只需调用 RouteService.CalculateRoute
方法即可。这是代码片段
//Parse user data to create array of waypoints
string[] points = new string[]{start, end};
Waypoint[] waypoints = new Waypoint[points.Length];
int pointIndex = -1;
foreach (string point in points)
{
pointIndex++;
waypoints[pointIndex] = new Waypoint();
string[] digits = point.Split(',');
waypoints[pointIndex].Location = new RouteService.Location();
waypoints[pointIndex].Location.Latitude = double.Parse(digits[0].Trim());
waypoints[pointIndex].Location.Longitude = double.Parse(digits[1].Trim());
if (pointIndex == 0)
waypoints[pointIndex].Description = "Start";
else if (pointIndex == points.Length)
waypoints[pointIndex].Description = "End"
else
waypoints[pointIndex].Description =
string.Format("Stop #{0}", pointIndex);
}
routeRequest.Waypoints = waypoints;
// Make the calculate route request
RouteServiceClient routeService = new RouteServiceClient();
RouteResponse routeResponse = routeService.CalculateRoute(routeRequest);
这是快照

关注点
现在您已经知道如何使用 Bing Maps Web 的 Geocode、Imagery、Route 和 Search 服务了。随意玩转代码并深入挖掘。玩得开心!
历史
- 2010 年 5 月 13 日:初始版本