Community of VE/MapPoint Users and Developers
This is a discussion on How do I get the driving distance of two addresses? within the MapPoint 2006/2009 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 ...
| |||||||
| Register | Blogs | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| |||
| How do I get the driving distance of two addresses? 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;
TIA |
| |||
|
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
|
| |||
| 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();
__________________ rgds, Wilfried Mestdagh www.mestdagh.biz MapPoint coding demo Order MapPoint 2009 with Routing and User Tools Spreadsheet |
| |||
| 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 |
| |||
| 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.
__________________ rgds, Wilfried Mestdagh www.mestdagh.biz MapPoint coding demo Order MapPoint 2009 with Routing and User Tools Spreadsheet |
![]() |
| Tags |
| addresses, distance, driving |
| ||||
| Posted By | For | Type | Date | |
| seattle-tacoma forums - craigslist | This thread | Refback | 12-19-2007 07:30 PM | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Find distance between two addresses | WonderMonkey | MapPoint 2006/2009 Discussion | 0 | 06-26-2006 03:28 PM |
| Driving distance between two postcodes | zippy | MapPoint 2006/2009 Discussion | 2 | 10-24-2004 11:56 PM |
| Fast Calculation of Driving Distance between 2 points | Bob Chase | Wish List | 6 | 08-19-2004 07:10 PM |
| SHORTEST DRIVING DISTANCE | Anonymous | MapPoint Web Service and Virtual Earth | 1 | 07-29-2004 08:51 PM |
| Calculate Distance Between 2 Street Addresses? | Anonymous | MapPoint 2006/2009 Discussion | 4 | 10-08-2003 06:16 AM |