在移动(Mobility)操作系统方面,的SmartPhone和PPC为微软建立起强大的地位。
在本文中,我们将基于MapPoint SDK,使用C#建立一些简单的应用程序。我们将先在微软SmartPhone上显示一个指定地点的地图。
我们先介绍一下MapPoint。MapPoint暴露了四个Web服务:
1.查找服务--帮助我们定位地址、检索经纬度、地理实体。
2.显示服务--这个服务允许我们显示指定地址的地图,并设置被显示地图的大小和视图。同时,我们还可以设定"图钉",可以作为阅读器的可视化标记。
3.路线服务--这个服务允许我们生成路线、计算两个地点之间的距离并提供驾车方向。
4.通用服务--它是一些工具,是上述的三个Web服务公用部分。提供一些服务,例如国家信息和地图数据源信息。
如果要使用MapPoint Web服务,你就必须获得一个开发者帐号。点击这个链接进行注册。你可以注册并收到一个评估帐号,如果你是MSDN订户,就可以收到为期1年的免费订阅。
好了,下面我们进行代码分析。
我会分解应用程序的代码。但是我强烈推荐你阅读MapPoint SDK的基础知识。解释这些相同的内容是重复的,没有增加任何价值。
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);
}
}
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对象,它获取被显示地图的地址详细信息。
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;
}