Click here to Skip to main content
Click here to Skip to main content

MapPoint, SmartPhone and C# - Part 2

, 10 Nov 2004
Rate this:
Please Sign up or sign in to vote.
A simple application that illustrates how to use MapPoint SDK using C#, and render the Route maps and calculate the distance on a SmartPhone.

Introduction

This is the 2nd part of the article series. Part 1 is available here.

In this Part 2 of the article, let us find the route between two addresses and calculate their distance.

Download and open the project solution. Now, open the MapPointWrapper.cs and replace your Map point developer username, password for the _mapPointUserName and _mapPointPassword const strings.

The Form1.cs contains a Menu object, and would get the address details for the map to be rendered.

On click of the “Get Route” menu, the address objects are created, and datasource is set to “MapPoint.NA”. Now, to find the route between two locations, we need to do the following:

  1. Identify the Latitude and Longitude of both the address
  2. Fetch the Map with the route embedded with pushpins

The following code gets the Latitude and Longitude of the address using the FindServiceSoap Web Service. The FindResults class has a property “LatLong” that would the give the latitude and longitude of the given address.

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);
  }
}

The obtained Latitude and Longitude is passed to the following method to obtain the map:

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;
}

The RouteMap is generated using the “RouteServiceSoap” webservice. After authenticating with the webservice, a header value information - DistanceUnit is set. This webservice provides the method “CalculateSimpleRoute” that would calculate the itinerary of a route based on an array of latitude and longitude coordinates.

This would return a route object. Also pushpins are created for the route. Again RenderServiceSoap Web Service is called, but this time the output is different. We would get a series of maps that is split from start to end of the map.

Here is the screenshot of the Map with routes displayed.

Screenshot of Maps with routes Screenshot of Maps with routes

This is collected on a bitmap array and shown on the PictureBox appropriately. The “route.Itinerary.Segments[0].Distance;” returns the distance between the two addresses. This distance can be obtained either as miles or as kilometers. This is set as the header value for the “RouteServiceSoap” webservice.

Here is a screenshot that displays the distance between the given addresses:

Screenshot - Display the total distance between addresses

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Logu Krishnan
Web Developer
India India
No Biography provided

Comments and Discussions

 
GeneralHelp Pinmemberrajpatil8-Jan-05 22:17 
GeneralHi Pinmember^^o000o^^19-Dec-04 7:24 
Questionsuggestions ??? Pinmembertom_dx10-Nov-04 12:37 
AnswerRe: suggestions ??? PinsussAnonymous10-Nov-04 18:26 
GeneralRe: suggestions ??? PinsussAnonymous10-Nov-04 18:26 
AnswerRe: suggestions ??? PinmemberLogu Krishnan11-Nov-04 5:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 10 Nov 2004
Article Copyright 2004 by Logu Krishnan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid