Welcome to MapForums!

Register, sign in, or use Facebook Connect above to join in and participate in the forum.

When you are signed in, this message, the ads in this row, and the red-underlined link ads all go away.

Subscribe to receive our newsletter.
Subscribe Unsubscribe
Results 1 to 6 of 6

How do I get the driving distance of two addresses?

This is a discussion on How do I get the driving distance of two addresses? within the MapPoint Desktop Discussion forums, part of the Map Forums category; How do I get the driving distance of two addresses using MapPoint 2004 COM Interface in C#.NET? I have already ...

  1. #1
    dawa is offline Junior Member White Belt
    Join Date
    Jan 2006
    Posts
    5

    How do I get the driving distance of two addresses?

    How do I get the driving distance of two addresses using MapPoint 2004 COM Interface in C#.NET?

    I have already added the reference to MapPoint COM inteface to the project.

    Below is my code which retrieves the distance (NOT Driving distance) between two locations:
    Code:
            try{
                if (this.mappointApplication == null) throw new Exception("Failed to create an object instance of MapPoint.MappointControlClass()");
                MapPoint.FindResults loResultsFrom = this.mappointApplication.ActiveMap.FindAddressResults(
                    stringFromAddress,
                    stringFromCity,
                    stringFromOtherCity,
                    stringFromRegion,
                    stringFromPostalCode,
                    stringFromCountry);
                if (loResultsFrom.ResultsQuality == MapPoint.GeoFindResultsQuality.geoAllResultsValid ||
                    loResultsFrom.ResultsQuality == MapPoint.GeoFindResultsQuality.geoFirstResultGood)
                {
                    MapPoint.FindResults loResultsTo = this.mappointApplication.ActiveMap.FindAddressResults(
                        stringToAddress,
                        stringToCity,
                        stringToOtherCity,
                        stringToRegion,
                        stringToPostalCode,
                        stringToCountry);
                    if (loResultsTo.ResultsQuality == MapPoint.GeoFindResultsQuality.geoAllResultsValid ||
                        loResultsTo.ResultsQuality == MapPoint.GeoFindResultsQuality.geoFirstResultGood)
                    {
                        object index = 1;
                        MapPoint.Location loFrom = (MapPoint.Location)loResultsFrom.get_Item(ref index);
                        MapPoint.Location loTo = (MapPoint.Location)loResultsTo.get_Item(ref index);
                        ldDistance = loTo.DistanceTo(loFrom);
                    }
                    else
                    {
                        throw new Exception("To address is not a valid one. Result Quality: " + loResultsTo.ResultsQuality.ToString());
                    }
                }
                else
                {
                    throw new Exception("From address is not a valid one. Result Quality: " + loResultsFrom.ResultsQuality.ToString());
                }
            }
            catch (Exception loExp)
            {
                string lcMessage = loExp.Message + ((char)13).ToString()
                                + loExp.Source + ((char)13).ToString()
                                + loExp.StackTrace + ((char)13).ToString()
                                + loExp.Source + ((char)13).ToString();
                EventLog.WriteLog(loExp);
                while (loExp.InnerException != null)
                {
                    EventLog.WriteLog(loExp);
                }
            }
            return ldDistance;
    How do I modify this code to get the driving distance of the two locations?

    TIA

  2. #2
    brianmcg is offline Junior Member Yellow Belt
    Join Date
    Jan 2003
    Posts
    17
    Hi Dawa,

    Your code is calculating the great circle distance between the two points, but to get MapPoint to tell you the driving distance, you'll have to get it to calculate the route between the two points first.

    After you create the location objects using FindAddressResults() method, use the ActiveMap.ActiveRoute.Waypoints.Add method to add these locations to the route, then call the .Calculate method to do all the calculations.

    The VB example here gets the route distance and the route time (based on the default driving speeds) between your two location objects (loFrom and loTo).

    Hope this helps.

    Code:
            map.ActiveMap.ActiveRoute.Clear
            map.ActiveMap.ActiveRoute.Waypoints.Add loFrom
            map.ActiveMap.ActiveRoute.Waypoints.Add loTo
            
            map.ActiveMap.ActiveRoute.Calculate
            
            nRouteDistance = map.ActiveMap.ActiveRoute.Distance
            nRouteTime = map.ActiveMap.ActiveRoute.DrivingTime * 24 * 60     '   convert to minutes
            
            map.ActiveMap.ActiveRoute.Clear
    Brian McGrath
    CAD North Inc.
    http://www.cadnorth.com

  3. #3
    lewisjohn78 is offline Junior Member White Belt
    Join Date
    Nov 2006
    Posts
    2

    Re: How do I get the driving distance of two addresses?

    Am new to vba but would like to do what you've done here? Can anyone give me an idiots guide?

  4. #4
    Wilfried is offline Senior Member Black Belt
    Join Date
    Nov 2004
    Location
    Belgium
    Posts
    2,433

    Re: How do I get the driving distance of two addresses?

    Hi,

    It looks you use the object model. Converting from VB to C# is only a little syntax difference. Something like this should work for you:

    Code:
    MapPoint.ActiveRoute.Clear;
    MapPoint.ActiveRoute.Waypoints.Add(loFrom);
    MapPoint.ActiveRoute.Waypoints.Add(locTo);
    MapPoint.ActiveRoute.Calculate();
    double RouteDistance = MapPoint.ActiveRoute.Distance;
    double RouteTime = MapPoint.ActiveRoute.DrivingTime * 24 * 60;
    MapPoint.ActiveRoute.Clear();

  5. #5
    lewisjohn78 is offline Junior Member White Belt
    Join Date
    Nov 2006
    Posts
    2

    Re: How do I get the driving distance of two addresses?

    Thank you for your reply. I've only ever used vba for excel macros- I could do with an idiots guide. Any chance you could brek it down for me? How do I create an object model? Sorry to be a pain! I would be very grateful
    Kind regards

  6. #6
    Wilfried is offline Senior Member Black Belt
    Join Date
    Nov 2004
    Location
    Belgium
    Posts
    2,433

    Re: How do I get the driving distance of two addresses?

    Hi,

    You use the object model witch is using the type library and another way is to use the activex component. The ActiveMap property of the control is the mapoint object.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find distance between two addresses
    By WonderMonkey in forum MapPoint Desktop Discussion
    Replies: 0
    Last Post: 06-26-2006, 03:28 PM
  2. Driving distance between two postcodes
    By zippy in forum MapPoint Desktop Discussion
    Replies: 2
    Last Post: 10-24-2004, 11:56 PM
  3. Replies: 6
    Last Post: 08-19-2004, 07:10 PM
  4. SHORTEST DRIVING DISTANCE
    By Anonymous in forum Bing Maps and MapPoint Web Service
    Replies: 1
    Last Post: 07-29-2004, 08:51 PM
  5. Calculate Distance Between 2 Street Addresses?
    By Anonymous in forum MapPoint Desktop Discussion
    Replies: 4
    Last Post: 10-08-2003, 06:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96