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 4 of 4

Automating Mappoint with Lat/Lon as input.

This is a discussion on Automating Mappoint with Lat/Lon as input. within the MapPoint Desktop Discussion forums, part of the Map Forums category; Hello, I am trying to automate mappoint in VBA to find the street distances and times between two Lat/Lon locations. ...

  1. #1
    mallettgt is offline Junior Member White Belt
    Join Date
    Mar 2007
    Posts
    2

    Automating Mappoint with Lat/Lon as input.

    Hello, I am trying to automate mappoint in VBA to find the street distances and times between two Lat/Lon locations. The assumption can be made that the variables for input are dblOriginLat, dblOriginLon, dblDestLat, dblDestLon. Ideally the output would be something along the lines of dblMiles and dblTime (in minutes). I will be running this within a loop so I need to do this in such a way that the "Route" is cleared after every two points so it can be run multiple times.

    I have found this code but not sure how to clear the Routes afterwards nor how to use it with Lat/Lon.

    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

    Thanks,
    Josh Mallett


  2. #2
    Paul Larson is offline Senior Member Green Belt
    Join Date
    Sep 2005
    Location
    Battle Creek, Michigan
    Posts
    136

    Re: Automating Mappoint with Lat/Lon as input.

    To clear the route, simply add the method
    objMap.ActiveRoute.Clear
    before or after your "With" block.

    To use it with latitude/longitude pairs, simply pass the latlon pair to the GetLocation() method.

    The time value returned is in fractional days, so simply perform some simply hour/minute/second multipliers to obtain the desired output.

    The distance value is returned as a decimal which is in in the current Units parameter of the Application object. Either geoMiles or geoKM.

    HTH
    Paul

  3. #3
    mallettgt is offline Junior Member White Belt
    Join Date
    Mar 2007
    Posts
    2

    Re: Automating Mappoint with Lat/Lon as input.

    Thanks Paul! Another question for whomever. Below is a snippet of the code that I am running within a loop. However, I need to find the distance between two points approximately 500,000 times. I know that no matter what you do it's going to take a significant amount of time to calculate, however, I was wondering if there is anything I could do to my code to ensure it run more efficient?

    Thanks,
    Josh

    Set objMap = objApp.ActiveMap
    Set objOrigin = objMap.GetLocation(dblOriginLat, dblOriginLon)
    Set objDest = objMap.GetLocation(dblDestLat, dblDestLon)
    objMap.ActiveRoute.Waypoints.Add objOrigin
    objMap.ActiveRoute.Waypoints.Add objDest
    objMap.ActiveRoute.Calculate
    dblRoadMiles = objMap.ActiveRoute.Distance
    dblTravelTime = objMap.ActiveRoute.TripTime
    objMap.ActiveRoute.Waypoints.Item(1).Delete
    objMap.ActiveRoute.Waypoints.Item(1).Delete

  4. #4
    Paul Larson is offline Senior Member Green Belt
    Join Date
    Sep 2005
    Location
    Battle Creek, Michigan
    Posts
    136

    Re: Automating Mappoint with Lat/Lon as input.

    1) Keep a static/global reference to the ActiveMap object instead of re-assigning each time.

    2) Don't bother deleting the waypoints individually. They will auto-delete when you clear the ActiveRoute.

    3) Consider doing this as a COM AddIn. That way it will execute in MapPoint's memory and process space, avoiding a TON of data marshalling.

    4) Avoid unnecessary inline assigment variables. You could code the waypoint addition as something like
    Code:
    objMap.ActiveRoute.WayPoints.Add(objMap.GetLocation(dblOriginLat,dblOriginLon))
    5) Depending on how you are getting your LatLong pairs into memory, you might benefit from using one of the ImportData methods to get them all into mappoint at one sweep.

    6) You'll get a slight performance gain if you order your input locations according to regions, i.e. computing all destinations in the state of Michigan, then proceeding to Ohio, etc. This prevents mappoint from having to re-buffer its underlying cartography data.

    7) Keep the mappoint application (or activex control) minimized or non-visible during the routing. This avoids excessive repaints of the window.

    There are a couple other minor performance gain methods, but these should provide a good start.


    HTH
    Paul

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. PLEASE HELP-Automating Access and Mappoint
    By rsilberfarb in forum MapPoint Desktop Discussion
    Replies: 1
    Last Post: 01-24-2007, 09:03 PM
  2. Automating Access and Mappoint
    By rbuonocore in forum MapPoint Desktop Discussion
    Replies: 19
    Last Post: 01-22-2007, 10:54 AM
  3. Automating Mappoint using VB
    By vennamreddy in forum MapPoint Desktop Discussion
    Replies: 1
    Last Post: 10-07-2004, 06:19 AM
  4. Automating Mappoint
    By Anonymous in forum MapPoint Desktop Discussion
    Replies: 3
    Last Post: 07-26-2004, 03:29 PM
  5. Automating MapPoint with the .NET SDK
    By Anonymous in forum MP2K Magazine Articles
    Replies: 2
    Last Post: 10-21-2002, 06:41 PM

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