PDA

View Full Version : How do I get the driving distance of two addresses?



dawa
01-04-2006, 11:47 AM
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:

try{
if (this.mappointApplication == null) throw new Exception("Failed to create an object instance of MapPoint.MappointControlClass()");
MapPoint.FindResults loResultsFrom = this.mappointApplication.ActiveMap.FindAddressResu lts(
stringFromAddress,
stringFromCity,
stringFromOtherCity,
stringFromRegion,
stringFromPostalCode,
stringFromCountry);
if (loResultsFrom.ResultsQuality == MapPoint.GeoFindResultsQuality.geoAllResultsValid ||
loResultsFrom.ResultsQuality == MapPoint.GeoFindResultsQuality.geoFirstResultGood)
{
MapPoint.FindResults loResultsTo = this.mappointApplication.ActiveMap.FindAddressResu lts(
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

brianmcg
01-08-2006, 10:23 PM
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.



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

lewisjohn78
04-15-2008, 05:33 AM
Am new to vba but would like to do what you've done here? Can anyone give me an idiots guide?

Wilfried
04-16-2008, 04:39 AM
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:


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

lewisjohn78
04-16-2008, 07:14 AM
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

Wilfried
04-18-2008, 01:23 AM
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.