65.9K
CodeProject 正在变化。 阅读更多。
Home

MapPoint、智能手机和 C# - 第 2 部分

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.75/5 (4投票s)

2004年11月10日

2分钟阅读

viewsIcon

63991

downloadIcon

171

一个简单的应用程序,演示了如何使用 C# 使用 MapPoint SDK,并在智能手机上渲染路线地图并计算距离。

引言

这是文章系列的第 2 部分。第 1 部分可在 此处 找到。

在本文的第 2 部分中,让我们查找两个地址之间的路线并计算它们的距离。

下载并打开项目解决方案。现在,打开 MapPointWrapper.cs 并将您的 MapPoint 开发者用户名和密码替换为 _mapPointUserName_mapPointPassword const 字符串。

Form1.cs 包含一个 Menu 对象,并将获取用于渲染地图的地址详细信息。

单击“获取路线”菜单时,将创建地址对象,并将数据源设置为“MapPoint.NA”。现在,要查找两个位置之间的路线,我们需要执行以下操作:

  1. 确定两个地址的纬度和经度
  2. 获取带有嵌入式图钉的地图

以下代码使用 FindServiceSoap Web Service 获取地址的纬度和经度。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”webservice 生成 RouteMap。在通过 webservice 身份验证后,设置一个头部值信息 - DistanceUnit。此 webservice 提供了“CalculateSimpleRoute”方法,该方法将根据一系列纬度和经度坐标计算路线的行程。

这将返回一个 route 对象。 并且为路线创建图钉。 再次调用 RenderServiceSoap Web Service,但这次输出不同。 我们将获得一系列从地图开始到结束拆分的地图。

这是显示路线的地图截图。

Screenshot of Maps with routes Screenshot of Maps with routes

这被收集到一个位图数组中,并以适当的方式显示在 PictureBox 上。“route.Itinerary.Segments[0].Distance;” 返回两个地址之间的距离。 可以以英里或公里为单位获得此距离。 这设置为“RouteServiceSoap”webservice 的头部值。

这是一张显示给定地址之间距离的截图

Screenshot - Display the total distance between addresses

© . All rights reserved.