天天看點

MapXtreme開發經驗分享-實作鷹眼

由于MapXtreme沒有提供鷹眼控件,需要自己來寫。實作鷹眼有兩個步驟:

1)在地圖的視圖改變的時候根據地圖目前的邊距生成一個矩形框,然後把這個矩形框繪制到鷹眼視窗上。

2)根據使用者在鷹眼視窗上點選的位置,同步顯示地圖視窗對應的位置。

VB.NET代碼如下:

    ' 更新鷹眼圖

    Private Sub UpdateEyeMap()

        Try

            Dim tblRect As Table

            tblRect = Session.Current.Catalog.GetTable("TempRect")

            If Not tblRect Is Nothing Then

                tblRect.Close()

            End If

            Dim tblInfo As TableInfo

            tblInfo = TableInfoFactory.CreateTemp("TempRect")

            Dim tblSessionInfo As TableSessionInfo = New TableSessionInfo()

            tblRect = Session.Current.Catalog.CreateTable(tblInfo, tblSessionInfo)

            Dim feaLayer As FeatureLayer = New FeatureLayer(tblRect)

            MapControl_EagleEye.Map.Layers.Add(feaLayer)

            '實時在鷹眼臨時表圖上畫矩形

            tblRect = Session.Current.Catalog.GetTable("TempRect")

            CType(tblRect, ITableFeatureCollection).Clear()

            '設定矩形的樣式

            Dim rect As MapInfo.Geometry.DRect = MapControl1.Map.Bounds

            Dim feageo As FeatureGeometry = New MapInfo.Geometry.Rectangle(MapControl1.Map.GetDisplayCoordSys(), rect)

            Dim simLineStyle As SimpleLineStyle = New SimpleLineStyle(New LineWidth(2, MapInfo.Styles.LineWidthUnit.Point), 2, System.Drawing.Color.Red)

            Dim simInterior As SimpleInterior = New SimpleInterior(9, System.Drawing.Color.Gray, System.Drawing.Color.Green, True)

            Dim comStyle As CompositeStyle = New CompositeStyle(New AreaStyle(simLineStyle, simInterior), Nothing, Nothing, Nothing)

            '将矩形插入到圖層中

            Dim fea As Feature = New Feature(feageo, comStyle)

            tblRect.InsertFeature(fea)

            '重新定位鷹眼圖的中心

            MapControl_EagleEye.Map.Center = Map.Center

            MapControl_EagleEye.Map.Layers("TempRect").Invalidate()

        Catch ex As Exception

            MessageBox.Show("鷹眼顯示錯誤:" + ex.Message)

        End Try

    End Sub

    Private Sub Map_ViewChanged(ByVal sender As System.Object, ByVal e As MapInfo.Mapping.ViewChangedEventArgs)

        UpdateEyeMap()

    End Sub

    Private Sub MapControl_EagleEye_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MapControl_EagleEye.Click

        Dim DisplayPoint As System.Drawing.PointF = New PointF(CType(e, System.Windows.Forms.MouseEventArgs).X, CType(e, System.Windows.Forms.MouseEventArgs).Y)

        Dim MapPoint As New MapInfo.Geometry.DPoint()

        Dim converter As MapInfo.Geometry.DisplayTransform = Me.MapControl_EagleEye.Map.DisplayTransform

        converter.FromDisplay(DisplayPoint, MapPoint)

        Map.Center = MapPoint

    End Sub