gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15951
  • QQ
  • 铜币25345枚
  • 威望15368点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
阅读:2088回复:4

[分享]寻找最短距离的MO代码

楼主#
更多 发布于:2005-01-15 10:23
<PRE>拷贝代码到窗体里,添加一个map和一个简单polygon图层到map上</PRE><PRE>enjoy!希望大家继续讨论</PRE><PRE>Option Explicit

' points collection to store the points from each of
' the polygons that are under the search point
' need a collection of points, since there may be
' overlapping polygons
Private m_NearestVertexs As MapObjects2.Points
' symbol to draw the nearest vertex with
Private m_NearestSymbol As MapObjects2.Symbol

Private Sub Form_Load()
    Set m_NearestSymbol = New MapObjects2.Symbol
    m_NearestSymbol.SymbolType = moPointSymbol
    m_NearestSymbol.Color = vbRed
    m_NearestSymbol.Size = 10
    m_NearestSymbol.Style = moTriangleMarker
    
    Set m_NearestVertexs = New MapObjects2.Points
    
End Sub

Private Sub Map1_AfterTrackingLayerDraw(ByVal hDC As stdole.OLE_HANDLE)
    If m_NearestVertexs.Count > 0 Then
        Map1.DrawShape m_NearestVertexs, m_NearestSymbol
    End If
End Sub

Private Sub Map1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim oSearchPoint As MapObjects2.Point
    Dim oRecs As MapObjects2.Recordset
    Dim oSearchLayer As MapObjects2.MapLayer
    Dim oPoints As MapObjects2.Points
    Dim oPoint As MapObjects2.Point
    Dim oPoly As MapObjects2.Polygon
    Dim oClosestPoint As MapObjects2.Point
    Dim dDist As Double
    Dim dTmp As Double
    
    Set oSearchPoint = Map1.ToMapPoint(X, Y)
    If oSearchPoint Is Nothing Then
        MsgBox "no search point"
        Exit Sub
    End If
    
    ' get the search layer
    Set oSearchLayer = Map1.Layers(0)
    
    ' find the polygons that contain the search point
    Set oRecs = oSearchLayer.SearchShape(oSearchPoint, moPointInPolygon, "")
    
    ' clear the existing points
    Do While m_NearestVertexs.Count > 0
        m_NearestVertexs.Remove 0
    Loop
    
    ' iterate over the polygons that the search point falls in
    Do While Not oRecs.EOF
        Set oPoly = oRecs.Fields("shape").Value
        ' start with a really large search dist
        dDist = 1000000000
        ' add the nearest point from the polygon to the points collection
        For Each oPoints In oPoly.Parts
            For Each oPoint In oPoints
                'Debug.Print oPoint.X, oPoint.Y
                dTmp = oPoint.DistanceTo(oSearchPoint)
                If dTmp < dDist Then
                    Set oClosestPoint = oPoint
                    dDist = dTmp
                End If
            Next
        Next
        m_NearestVertexs.Add oClosestPoint
        oRecs.MoveNext
    Loop
    Map1.TrackingLayer.Refresh True
End Sub

</PRE>
喜欢0 评分0
GIS麦田守望者,期待与您交流。
gisfinder
路人甲
路人甲
  • 注册日期2004-12-13
  • 发帖数20
  • QQ
  • 铜币167枚
  • 威望0点
  • 贡献值0点
  • 银元0个
1楼#
发布于:2005-02-28 16:02
<P>我看了这段代码,好像不是找最短路径吧?</P><P>是找距离地图上面状元素上的点,这点距离已知点路径最近,不是最短路径 。</P>
举报 回复(0) 喜欢(0)     评分
zchang81
路人甲
路人甲
  • 注册日期2005-05-11
  • 发帖数95
  • QQ
  • 铜币324枚
  • 威望0点
  • 贡献值0点
  • 银元0个
2楼#
发布于:2005-06-28 18:17
<img src="images/post/smile/dvbbs/em01.gif" />
举报 回复(0) 喜欢(0)     评分
heqjxiaoyao
路人甲
路人甲
  • 注册日期2003-07-31
  • 发帖数981
  • QQ83031582
  • 铜币910枚
  • 威望0点
  • 贡献值0点
  • 银元0个
3楼#
发布于:2005-06-28 18:54
<P>这是搜索最近的地物特征点</P>
希望大家访问我的个人博客: 随笔闲谈: http://rsgisman.bokee.com
举报 回复(0) 喜欢(0)     评分
zpgis2002
路人甲
路人甲
  • 注册日期2004-10-29
  • 发帖数83
  • QQ
  • 铜币329枚
  • 威望0点
  • 贡献值0点
  • 银元0个
4楼#
发布于:2005-12-26 23:04
可以说是最短距离,但是只是在搜索集合中所有点距离oSearchPoint的距离最短
举报 回复(0) 喜欢(0)     评分
游客

返回顶部