简体   繁体   English

MapPoint:在一个州内行驶的距离

[英]MapPoint: Distances traveled within a state

I would like a process to take two points and build a route between them, and be able to tally miles by state. 我希望有一个过程可以得出两点并在它们之间建立一条路线,并能够按州统计里程。 would a process like this be possible within Access VBA or VB.net? 在Access VBA或VB.net中是否可以进行这样的处理?

So far I am able to make a route and get the distance, but I am not sure what would be next to find states traveled and the tally of the miles in each state. 到目前为止,我已经能够确定路线并获取距离,但是我不确定接下来要查找的州和每个州的里程计数将是什么。

Here is what I have so far: 这是我到目前为止的内容:

Public Sub route()


  Dim objApp As New MapPoint.Application
  Dim objMap As MapPoint.Map
  Dim objRoute As MapPoint.route


  'Set up application
  Set objMap = objApp.ActiveMap
  Set objRoute = objMap.ActiveRoute
  objApp.Visible = True
  objApp.UserControl = True


  'Add route stops and calculate the route
  objRoute.Waypoints.Add objMap.FindResults("Iron Mountain, MI").Item(1)
  objRoute.Waypoints.Add objMap.FindResults("Chicago, IL").Item(1)
  objRoute.Calculate

    For Each Item In objRoute.Directions
        MsgBox Item.Instruction & " " & Item.ElapsedDistance
    Next

End Sub

An Issue I am still having is I have tried to use Item.Location.StreetAddress.Region but I get: "Object Variable or With block variable not set." 我仍然遇到的一个问题是我尝试使用Item.Location.StreetAddress.Region,但是得到:“对象变量或未设置块变量”。 Can anyone tell me how to find the state of a particular direction point? 谁能告诉我如何找到特定方向点的状态? If so I beleive I can find elapsed miles by state by adding elapsed miles - previous elapsed miles to the state it belongs to. 如果是这样,我相信我可以通过添加“已行驶里程”来找到各州的已行驶里程,“已行驶里程”是其所属州以前的已行驶里程。

thanks! 谢谢!

The actual error you are seeing is due to an abuse of the FindResults collection. 您看到的实际错误是由于滥用FindResults集合造成的。 You are blindly assuming that element 1 is set. 您盲目地假设已设置元素1。 This is not necessarily the case, and you should check the ResultsQuality property first. 不一定是这种情况,您应该首先检查ResultsQuality属性。 This is a very common problem that often appears on the various online forums - so common, that I wrote a quick article about it here: 这是一个非常普遍的问题,经常出现在各种在线论坛上-非常普遍,我在这里写了一篇简短的文章:

http://www.mapping-tools.com/howto/programming/using-the-findresults-collection/ http://www.mapping-tools.com/howto/programming/using-the-findresults-collection/

For City,State locations, you will probably also do better with FindAddressResults. 对于城市,州的位置,使用FindAddressResults可能也会做得更好。 This will result "Ambiguous Results" in most cases, but you can loop through the results and choose the first city (typically there are two results: City and State). 在大多数情况下,这将导致“模棱两可的结果”,但是您可以循环浏览结果并选择第一个城市(通常有两个结果:City和State)。


The above was in answer to the second part of the question. 以上是对问题第二部分的回答。 The follow was my response to the general strategy. 以下是我对总体战略的回应。 From the comments, it looks like the OP has worked it out, but I'm keeping it here so that the comments make sense. 从评论看来,OP似乎已经解决了问题,但我将其保留在此处,以便使评论有意义。

I don't think you can do what you want unless you internally had a list of shape definitions and did a point-in-polygon test (yes a lot of programming). 我认为除非您内部有一个形状定义列表并进行了多边形点测试(是很多编程工作),否则您无法做您想做的事情。

The Location's StreetAddress property is only set if the Location object was explicitly created from a street address (eg. At import). 仅当Location对象是从街道地址显式创建的(例如,在导入时),才设置Location的StreetAddress属性。

Note also that your method could have some large errors because there is no guarantee that a Direction lies close to a State boundary. 还要注意,您的方法可能会出现一些大错误,因为无法保证“方向”位于州边界附近。 Also, the Direction Location objects do not lie on the route - they are intended for viewing only. 同样,“方向位置”对象也不位于路线上-它们仅用于查看。 Yes sometimes they do lie on the route segment, but there is no guarantee whatsoever. 是的,有时它们确实位于路线段上,但是并不能保证。 The idea is that you can zoom to the Location, and get an ideal map view for that Direction (Turn Left, etc). 这个想法是您可以缩放到“位置”,并获得该方向的理想地图视图(向左转等)。

I thought it actually adds a Direction when it changes state, so it shouldn't be that inaccurate? 我以为它在更改状态时实际上会添加一个Direction,所以它不应该是不准确的吗? I know for accounting purposes tallying miles +/- a few bucks doesn't make much of a difference, and it's likely to be more accurate than relying on drivers to remember to look down and properly record mileage when crossing state lines. 我知道,出于会计目的,行驶里程+/-几块钱并没有多大区别,而且它可能比依靠驾驶员记住越过状态线时往下看并正确记录里程更准确。 In any case, you could add rest stops to force additional directions to be added to the route, this might make it slightly more accurate -- http://www.mp2kmag.com/update/mappoint.newsletter/2011-05-05/ 无论如何,您都可以添加停靠点以强制向路线添加其他路线,这可能会使路线更加准确-http: //www.mp2kmag.com/update/mappoint.newsletter/2011-05-05 /
Eric 埃里克
m: 312-399-1586 手机:312-399-1586

I have been tinkering with the following Access VBA code. 我一直在修改以下Access VBA代码。 It is an extension of the code in the question, but I used GetObject() so I could attach to an existing instance of MapPoint. 它是问题代码的扩展,但我使用了GetObject()因此可以附加到MapPoint的现有实例。 That way I could create my route in MapPoint (and tweak as necessary), then [Alt-Tab] into Access and run the code. 这样,我可以在MapPoint中创建路线(并根据需要进行调整),然后将[Alt-Tab]插入Access并运行代码。

The code is by no means complete, but it does seem to cover the basics. 该代码绝不是完整的,但似乎涵盖了基础知识。 As a test, I planned a route from "New York, NY" to "Los Angeles, CA" and when I run my VBA code the following appears in the Immediate Window of the VBA editor: 作为测试,我计划了一条从“纽约,纽约”到“加利福尼亚州洛杉矶”的路线,当我运行VBA代码时,以下内容将显示在VBA编辑器的“即时窗口”中:

"Origin_State: New York",0
"Entering New Jersey",1.57828283309937
"Entering Pennsylvania",76.8766632080078
"Entering Ohio",387.730041503906
"Entering Indiana",624.323974609375
"Entering Illinois",776.259155273438
"Entering Iowa",939.418151855469
"Entering Nebraska",1245.23413085938
"Entering Colorado",1599.96252441406
"Entering Utah",2054.32885742188
"Entering Arizona",2418.78686523438
"Entering Nevada",2448.091796875
"Entering California",2572.029296875
"End_of_route",2798.63793945313

The code is as follows: 代码如下:

Option Compare Database
Option Explicit

Dim objApp As MapPoint.Application
Dim objMap As MapPoint.Map

Const DebugMode = True  '' controls how error messages are displayed

Public Sub RouteTest()
    Dim objRoute As MapPoint.Route
    Dim objDirection As MapPoint.Direction
    Dim StateOfOrigin As String

    On Error GoTo RouteTest_Error
    '' attach to existing instance of MapPoint
    Set objApp = GetObject(, "MapPoint.Application")
    On Error GoTo 0  '' for debugging
    Set objMap = objApp.ActiveMap
    Set objRoute = objMap.ActiveRoute

    If objRoute.Directions Is Nothing Then
        DisplayErrorMessage "No route.", vbExclamation
        Exit Sub
    End If

    StateOfOrigin = GetState(objRoute.Directions(1).Location)
    Debug.Print """Origin_State: " & StateOfOrigin & """,0"

    For Each objDirection In objRoute.Directions
        If objDirection.Instruction Like "Entering *" Then
            Debug.Print """" & Replace(objDirection.Instruction, """", """""", 1, -1, vbBinaryCompare) & """," & objDirection.ElapsedDistance
        End If
    Next
    Set objDirection = objRoute.Directions(objRoute.Directions.Count)
    Debug.Print """End_of_route""," & objDirection.ElapsedDistance
    Set objDirection = Nothing

    Set objRoute = Nothing
    Set objMap = Nothing
    Set objApp = Nothing
    Exit Sub

    RouteTest_Error:
    If Err.Number = 429 Then
        DisplayErrorMessage "Unable to attach to existing instance of MapPoint.", vbCritical
    Else
        Err.Raise Err.Number
    End If
End Sub

Public Function GetState(loc As MapPoint.Location) As String
    '' adapted from code at http://www.mp2kmag.com/articles.asp?ArticleID=47
    Dim objResults As MapPoint.FindResults
    Dim objTempLoc As MapPoint.Location
    Dim rgn As String
    rgn = ""
    loc.Goto
    objMap.Altitude = 1
    Set objResults = objMap.ObjectsFromPoint(objMap.LocationToX(loc), objMap.LocationToY(loc))
    If objResults.ResultsQuality = geoAllResultsValid Then
        For Each objTempLoc In objResults
            If objTempLoc.Type = geoShowByRegion1 Then
                rgn = objTempLoc.Name
                Exit For
            End If
        Next
        Set objTempLoc = Nothing
    End If
    Set objResults = Nothing
    GetState = rgn
End Function

Private Sub DisplayErrorMessage(ErrorMessage As String, MessageBoxStyle As Long)
    If DebugMode Then
        Debug.Print ErrorMessage
    Else
        MsgBox ErrorMessage, MessageBoxStyle
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM