Latitude & Longitude into a function

pinem1
02-15-2007, 11:52 PM
I have been using the following code to help me determine the distances between stores. It works great, but I want to be more accurate and I want to use Latitude and Longitude.

Can someone help me modify the code so I can bring in a latitude and longitude for each of the two locations and then have it tell me the distance between the two?


Function MPRouteDist(iMPType As Integer, ParamArray WPoints())

Dim objApp As New MapPoint.Application
Set objMap = objApp.ActiveMap

With objMap.ActiveRoute
For Each wpoint In WPoints
.Waypoints.Add objMap.FindResults(wpoint).Item(1)
Next
.Waypoints.Item(1).SegmentPreferences = iMPType
.Calculate
MPRouteDist = Application.Round(CStr(.Distance), 5)
End With

objMap.Saved = True
End Function

A little bonus help as well. Is there a way to take the same information and have Mappoint tell me the minutes it would take to drive between the two locations?

Any and all help is greatly appreciated! Thanks

Eric Frost
02-16-2007, 06:38 AM
Well let's tackle the first one first.

Waypoint.Add is expecting a Location or Pushpin object, see the Help file.

Actually, check out the help file for Add Waypoints, they have an example right there --


Set objLoc = objMap.GetLocation(47.6008, -122.334, 100)

'Add a waypoint using the location and give it a name
objRoute.Waypoints.Add objLoc, "Seattle"



objLoc is a MapPoint.Location.

Eric

Eric Frost
02-16-2007, 06:59 AM
I didn't test this but see how this works for you.

Let us know.


Function MPRouteDist(iMPType As Integer, LatitudeStart As Long, LongitudeStart As Long, LatitudeEnd As Long, LongitudeEnd As Long )

Dim objApp As New MapPoint.Application
Set objMap = objApp.ActiveMap
Dim objLocStart As MapPoint.Location
Dim objLocEnd As MapPoint.Location

Set objLocStart = objMap.GetLocation(LatitudeStart, LongitudeStart, 100)
Set objLocEnd = objMap.GetLocation(LatitudeEnd, LongitudeEnd, 100)

With objMap.ActiveRoute
.Waypoints.Add objLocStart
.Waypoints.Add objLocEnd
.Waypoints.Item(1).SegmentPreferences = iMPType
.Calculate
MPRouteDist = Application.Round(CStr(.Distance), 5)
End With

objMap.Saved = True
End Function

pinem1
02-18-2007, 06:29 PM
my results all came in at 0. i don't think it worked quite right

Eric Frost
02-18-2007, 06:49 PM
Hmm, it worked for me.

I put this in the Excel debugger:

? MPRouteDist(1,40,-90,41,-90)
79.8462

Also, see the attached spreadsheet and screenshot.

hope this helps!

Eric

pinem1
02-18-2007, 08:31 PM
I think I fixed it. The Latitudes and Longitudes I am using have multiple decimals points. So I changed Long to Double and it seems to work now.

Thanks for all of your help

 
Web mp2kmag.com
mapforums.com