MapPoint+SmartPhone+C#开发示例
http://tech.ddvip.com 2006年08月02日 社区交流
本文详细介绍MapPoint+SmartPhone+C#开发示例
获取到的纬度和经度被传递到下面的方法中以获取地图:
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服务的头部值中。
下面是一个显示了两个地址之间距离的截屏。
来源:天极开发 作者:陶刚 责编:豆豆技术应用