科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件MapPoint SmartPhone C#开发示例

MapPoint SmartPhone C#开发示例

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

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

作者:朱先忠编译 来源:天极开发 2007年11月12日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
计算路线和距离

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

  下载并打开项目解决方案。打开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服务的头部值中。

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

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章