|
阅读:2088回复:4
[分享]寻找最短距离的MO代码
<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> |
|
|
|
1楼#
发布于:2005-12-26 23:04
可以说是最短距离,但是只是在搜索集合中所有点距离oSearchPoint的距离最短
|
|
|
2楼#
发布于:2005-06-28 18:54
<P>这是搜索最近的地物特征点</P>
|
|
|
|
3楼#
发布于:2005-06-28 18:17
<img src="images/post/smile/dvbbs/em01.gif" />
|
|
|
4楼#
发布于:2005-02-28 16:02
<P>我看了这段代码,好像不是找最短路径吧?</P><P>是找距离地图上面状元素上的点,这点距离已知点路径最近,不是最短路径 。</P>
|
|