MapPoint+SmartPhone+C#开发示例

2016-02-19 15:53 4 1 收藏

今天图老师小编给大家展示的是MapPoint+SmartPhone+C#开发示例,精心挑选的内容希望大家多多支持、多多分享,喜欢就赶紧get哦!

【 tulaoshi.com - 编程语言 】

  本文提供了一些简单的应用程序,它演示了C#如何使用MapPoint SDK,以及如何在SmartPhone上显示地图。

  介绍

  在业界迅速迁移到移动领域以获取相关利润的过程中,基于位置的服务和应用很明显领先一步。在技术前端,微软已经在所有这些相关技术领域中建立了牢固的地位。

  对于基于位置的应用程序来说,新的MapPoint SDK 3.5提供了一个XML Web服务,使我们能够建立起基于位置的服务。这个SDK帮助我们获取高质量的地图,该地图还标识出了特定的位置(例如pizza店、医院等),还提供了到达目把地点的路线图。你可以下载这个SDK。

  在移动(Mobility)操作系统方面,的SmartPhone和PPC为微软建立起强大的地位。

  在本文中,我们将基于MapPoint SDK,使用C#建立一些简单的应用程序。我们将先在微软SmartPhone上显示一个指定地点的地图。

  我们先介绍一下MapPoint。MapPoint暴露了四个Web服务:

  1.查找服务--帮助我们定位地址、检索经纬度、地理实体。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  2.显示服务--这个服务允许我们显示指定地址的地图,并设置被显示地图的大小和视图。同时,我们还可以设定"图钉",可以作为阅读器的可视化标记。

  3.路线服务--这个服务允许我们生成路线、计算两个地点之间的距离并提供驾车方向。

  4.通用服务--它是一些工具,是上述的三个Web服务公用部分。提供一些服务,例如国家信息和地图数据源信息。

  如果要使用MapPoint Web服务,你就必须获得一个开发者帐号。点击这个链接进行注册。你可以注册并收到一个评估帐号,如果你是MSDN订户,就可以收到为期1年的免费订阅。

  好了,下面我们进行代码分析。

  我会分解应用程序的代码。但是我强烈推荐你阅读MapPoint SDK的基础知识。解释这些相同的内容是重复的,没有增加任何价值。

  显示指定的地图

  下载并打开项目解决方案。现在,打开MapPointWrapper.cs,用你自己的MapPoint开发者用户名、密码代替_mapPointUserName和_mapPointPassword常量字符串。

  Form1.cs包含一个Menu对象,它将获取被显示的地图的地址的详细信息。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  点击"Get Map"菜单,会建立一个address对象,它已经被设置为"MapPoint.NA"。下面是一些可以使用的数据源。

  1. MapPoint.EU - 欧洲

  2. MapPoint.NA - 北美洲

  3. MapPoint.BR - 巴西

  4. MapPoint.World - 世界

  5. MapPoint.Moon - 月脉图

  下面的代码将使用FindServiceSoap Web服务,根据指定的地址检索位置信息。该Web服务必须通过MapPoint开发者帐号认证。我们必须提供数据源名称和地址。

  

public static void GetAddress(Address address, string DataSourceName,out indResults Location, out ViewByHeightWidth[] Views){ try {  FindServiceSoap locationService = new FindServiceSoap();  locationService.Credentials = new System.Net.NetworkCredential(_mapPointUserName,_mapPointPassword);  locationService.PreAuthenticate = true;  FindAddressSpecification locationData = new FindAddressSpecification();  locationData.DataSourceName = DataSourceName;  locationData.InputAddress = address;  Location = locationService.FindAddress(locationData);  Views = new ViewByHeightWidth[1];  Views[0] = Location.Results[0].FoundLocation.BestMapView.ByHeightWidth; } catch(Exception ex) {  throw new Exception(ex.Message,ex); }}

  在获取位置的详细信息之后,这些数据会被进一步发送给GetMap方法。这个方法使用了"RenderServiceSoap"Web服务。这个服务也需要认证信息。MapPoint提供了"图钉",它是一种用于标识地图上的地址的可视化标记。你可以从默认的图标组中选择一个,并设置一个恰当的名称。此外,它还会建立一个MapSpecification对象,它会保持视图、图钉、图像格式等内容。我们调用RenderService的GetMap方法来检索适当的图像,它是作为流检索到的,并用位图显示出来。

  

public static Bitmap GetMap(FindResults Location, ViewByHeightWidth[] Views,string DataSourceName,Point MapDimensions){ try {  RenderServiceSoap renderService = new RenderServiceSoap();  Pushpin[] pushpins = new Pushpin[1];  MapSpecification mapSpec = new MapSpecification();  renderService.Credentials = new System.Net.NetworkCredential(_mapPointUserName, _mapPointPassword);  renderService.PreAuthenticate = true;  pushpins[0] = new Pushpin();  pushpins[0].IconDataSource = "MapPoint.Icons";  pushpins[0].IconName = "0";  pushpins[0].Label = Location.Results[0].FoundLocation.Entity.Name;  pushpins[0].LatLong = Views[0].CenterPoint;  pushpins[0].ReturnsHotArea = true;  mapSpec.DataSourceName = DataSourceName;  mapSpec.Views = Views;  mapSpec.Pushpins = pushpins;  mapSpec.Options = new MapOptions();  mapSpec.Options.Format = new ImageFormat();  mapSpec.Options.Format.Width = MapDimensions.X;  mapSpec.Options.Format.Height = MapDimensions.Y;  MapImage[] mapImages = renderService.GetMap(mapSpec);  System.IO.Stream streamImage = new System.IO.MemoryStream(mapImages[0].MimeData.Bits);  Bitmap bitmap = new Bitmap(streamImage);  return bitmap; } catch(Exception ex) {  throw new Exception(ex.Message,ex); }}

本文示例代码或素材下载

  我们已经完成了事务。下面就是显示的地图。请注意地图上的"图钉"标识出正确的位置了。

  计算路线和距离

  下面我们将查找两个地址之间的路线,并计算它们之间的距离。

  下载并打开项目解决方案。打开MapPointWrapper.cs并用你自己的MapPoint用户名和密码代替_mapPointUserName和_mapPointPassword常量字符串。

  Form1.cs包含一个Menu对象,它获取被显示地图的地址详细信息。

  点击"Get Route"菜单的时候,会建立地址对象,并把数据源设置为"MapPoint.NA"。现在,我们来查找两个位置之间的路线,需要执行下面一些事务:

  1.识别出地址的纬度和经度。

  2.获取图钉所标识的路线的地图。

  下面的代码使用FindServiceSoap Web服务获取了地址的纬度和经度。FindResults类有属性"LatLong",它会给出给定地址的纬度和经度。

  

public static LatLong GetAddress(Address address, string DataSourceName,out FindResults Location, out ViewByHeightWidth[] Views){ try {  FindServiceSoap locationService = new FindServiceSoap();  locationService.Credentials = new System.Net.NetworkCredential(_mapPointUserName, _mapPointPassword);  locationService.PreAuthenticate = true;  FindAddressSpecification locationData = new FindAddressSpecification();  locationData.DataSourceName = DataSourceName;  locationData.InputAddress = address;  Location = locationService.FindAddress(locationData);  Views = new ViewByHeightWidth[1];  Views[0] = Location.Results[0].FoundLocation.BestMapView.ByHeightWidth;  return Location.Results[0].FoundLocation.LatLong; } catch(Exception ex) {  throw new Exception(ex.Message,ex); }}

  获取到的纬度和经度被传递到下面的方法中以获取地图:

  

public static double GetMapForRoute(out Bitmap[] RouteMaps,out ViewByHeightWidth[] Views, LatLong[] LatitudeLongitude,string DataSourceName, Point MapDimension){ RouteServiceSoap routeService = new RouteServiceSoap(); routeService.Credentials = new System.Net.NetworkCredential(_mapPointUserName,_mapPointPassword); routeService.PreAuthenticate = true; UserInfoRouteHeader routeUserInfo = new UserInfoRouteHeader(); routeUserInfo.DefaultDistanceUnit = DistanceUnit.Kilometer; routeService.UserInfoRouteHeaderValue = routeUserInfo; MapOptions mapOptions = new MapOptions(); mapOptions.Format = new ImageFormat(); mapOptions.Format.Width = MapDimension.X; mapOptions.Format.Height = MapDimension.Y; Route route; route = routeService.CalculateSimpleRoute(LatitudeLongitude, DataSourceName, SegmentPreference.Quickest); int MapDirectionLength = route.Itinerary.Segments[0].Directions.Length + 1; Views = new ViewByHeightWidth[MapDirectionLength]; RouteMaps = new Bitmap[MapDirectionLength]; Pushpin[] pushpins = new Pushpin[MapDirectionLength]; for (int idx = 0; idx = MapDirectionLength-1; idx++) {  pushpins[idx] = new Pushpin();  pushpins[idx].IconDataSource = "MapPoint.Icons";  if(idx != MapDirectionLength-1)  {   Views[idx] = route.Itinerary.Segments[0].Directions[idx].View.ByHeightWidth;   pushpins[idx].IconName = "0";   pushpins[idx].LatLong = route.Itinerary.Segments[0].Directions[idx].LatLong;  }  else  {   Views[idx] = route.Itinerary.Segments[1].Directions[0].View.ByHeightWidth;   pushpins[idx].IconName = "1";   pushpins[idx].LatLong =   route.Itinerary.Segments[1].Directions[0].LatLong;  }  pushpins[idx].ReturnsHotArea = true; } MapSpecification MapSpec = new MapSpecification(); MapSpec.DataSourceName = DataSourceName; MapSpec.Options = mapOptions; MapSpec.Views = Views; MapSpec.Pushpins = pushpins; MapSpec.Route = route; MapImage[] MapImages; RenderServiceSoap renderService = new RenderServiceSoap(); renderService.Credentials = new System.Net.NetworkCredential(_mapPointUserName,_mapPointPassword); renderService.PreAuthenticate = true; MapImages = renderService.GetMap(MapSpec); for (int idx = 0; idx  MapDirectionLength; idx++) {  RouteMaps[idx] = new Bitmap(new System.IO.MemoryStream(MapImages[idx].MimeData.Bits)); } return route.Itinerary.Segments[0].Distance;}

  我们使用"RouteServiceSoap"Web服务来生成RouteMap。在认证之后,就设置好了头部值信息DistanceUnit。该Web服务提供了一个"CalculateSimpleRoute"方法,它会根据相应的纬度和经度数组计算出路线。

  它会返回一个route对象。同时还为路线建立了"图钉"。它再次调用RenderServiceSoap Web服务,但是这次的输出内容是不同的。我们将得到一个地图序列,从地图的开始到末尾。

  下面是显示了路线的地图截屏。

  它是在位图数组上收集并适当地显示在PictureBox上的。"route.Itinerary.Segments[0].Distance;"返回两个地址之间的距离。这个距离可以以英里或公里为单位。其设定在RouteServiceSoap的Web服务的头部值中。

  下面是一个显示了两个地址之间距离的截屏。

本文示例代码或素材下载

来源:https://www.tulaoshi.com/n/20160219/1610636.html

延伸阅读
摘要:学习如何使用C# Builder构造一个简单的.RTF浏览程序以及C# Builder IDE的一些基本知识。 你想成为一名C#程序员吗?让我们开始创建一个Windows图形用户界面应用程序(使用C# Builder,你可以创建控制台应用程序、Web应用程序(ASP.NET)、WebMethod(亦称Web服务),也可以把数据库连接整合到ADO.NET中,但是出于简单的理由,我...
为何学习iOS开发要先学C语言呢,学习c语言对ios开发有何作用。 现在越来越多的iOS开发兴趣爱好者投入到了iOS培训中,有的是已经在职的员工,有的是还在就读的学生,还有一些是完全零基础的同学,那么对于他们来说就要一切从基础开始学,首先从C语言学起。 因为C语言是一门语言基础,因具备简洁、灵活等特点而被广泛应用,在多种开发环境中都...
在本文里,通过编辑一个小小的可以将温度从摄氏转换到华氏的程序,我们将亲身体会到C# GUI开发过程。阅读本文最基本的要求是您要具有C#及面向对象程序设计的基本知识。本文的写作宗旨是介绍C#,如果您希望编译或是运行本文所列举的程序,则需要有.NET Framework SDK的支持。 创建一个视窗应用程序主要包括以下的基本步骤:创建适合...
定位获取位置及位置编码-反编码 我们的应用程序,可以通过添加Core Location框架所包含的类,获取设备的地图位置。 添加CoreLocation.framework框架,导入#importCoreLocation/CoreLocation.h。 使用地图服务时,会消耗更多地设备电量.因此,在获取到设备的位置后,应该停止定位来节省电量。 我们通过一个demo来展示内容与效果 代码如下: // //...
设置你的Shell 原文:http://www.redhat.com/developer/whitepapers/ intro_dev/shell.Html 作者:Nathan Thomas nthomas@redhat.com 翻译:yellowrose yellowrose@fun.21cn.com 版权:Red Hat Software,Inc.本文英文版遵循GNU GPL版本2 或更新版本,参见www.gnu.org/copyleft/gpl.html.中文版 为...

经验教程

305

收藏

81
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部