ThinkGeo.com    |     Documentation    |     Premium Support

Add labels to a layer

I have the following code and do not seems to able to get labels to show ??


Regards


Ewan


 


 


    Private Sub New_Raster_Position(ByVal Unit_Name As String, ByVal Unit_Operator As String, ByVal Lat As Double, ByVal Lon As Double, ByVal Speed As Double, ByVal Direction As Integer, ByVal Status As String)



        Dim IconPath As String = Get_GIS_Icon(Unit_Name)



        If System.IO.File.Exists(IconPath) = False Then

            IconPath = AppPath & "\icons\gis\base.png"

        End If



        gisMAP.Overlays("iconOverlay").Lock.EnterWriteLock()



        Try



            Dim myFeature As New Feature(Lon, Lat, Unit_Name)

            myFeature.ColumnValues.Add("Unit_Name", Unit_Name)



            Dim iconLayer As InMemoryFeatureLayer = DirectCast(gisMAP.FindFeatureLayer("iconLayer"), InMemoryFeatureLayer)

            iconLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.Image = New GeoImage(IconPath)



            iconLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.City1("Unit_Name")

            iconLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20



            If Not iconLayer.InternalFeatures.Contains(Unit_Name) Then

                iconLayer.InternalFeatures.Add(Unit_Name, myFeature)

            Else

                iconLayer.Open()

                iconLayer.EditTools.BeginTransaction()

                iconLayer.EditTools.Delete(Unit_Name)

                iconLayer.EditTools.CommitTransaction()

                iconLayer.Close()

                iconLayer.InternalFeatures.Add(Unit_Name, myFeature)

            End If



        Finally

            gisMAP.Overlays("iconOverlay").Lock.ExitWriteLock()

        End Try

        gisMAP.Refresh()



    End Sub



Ewan,


I suspect the problem is when you create the InmemoryFeatureLayer , you did NOT set the featureSource columns correctly.
 
Please try to add the following code when you create the inmemoryFeatureLayer
 

Dim c As FeatureSourceColumn = New FeatureSourceColumn("Unit_Name", "String", 0)
Dim columns As New Collection(Of FeatureSourceColumn)()
columns.Add(c)

Dim bitmapLayer As New InMemoryFeatureLayer(columns, New Feature() {})

 

Any more questions just let me know.
 
Thanks.
 
Yale

 

OK I have the multiple icons now showing but cannot figure out how to labels them ?? 
  
 Thanks 
  
  
 Ewan 
  
     Private Sub New_Raster_Position(ByVal Unit_Name As String, ByVal Unit_Operator As String, ByVal Lat As Double, ByVal Lon As Double, ByVal Speed As Double, ByVal Direction As Integer, ByVal Status As String) 
  
         Dim IconPath As String = Get_GIS_Icon(Unit_Name) 
  
         If System.IO.File.Exists(IconPath) = False Then 
             IconPath = AppPath & “\icons\gis\base.png” 
         End If 
  
         gisMAP.Overlays(“iconOverlay”).Lock.EnterWriteLock() 
  
         Try 
             Dim myMapShape As New MapShape(New Feature(Lon, Lat, Unit_Name)) 
  
             myMapShape.ZoomLevels.ZoomLevel01.DefaultPointStyle = New PointStyle(New GeoImage(IconPath)) 
             myMapShape.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20 
  
             If Not iconLayer.MapShapes.ContainsKey(Unit_Name) Then 
                 iconLayer.MapShapes.Add(Unit_Name, myMapShape) 
             Else 
                 iconLayer.Open() 
                 iconLayer.MapShapes.Remove(Unit_Name) 
                 iconLayer.Close() 
                 iconLayer.MapShapes.Add(Unit_Name, myMapShape) 
             End If 
  
         Finally 
             gisMAP.Overlays(“iconOverlay”).Lock.ExitWriteLock() 
         End Try 
  
         gisMAP.Refresh() 
  
     End Sub 
  
 Public Class MapShape 
  
     Private m_feature As Feature 
     Private zoomLevelSet As ZoomLevelSet 
  
     Public Sub New() 
         Me.New(New Feature()) 
     End Sub 
  
     ’ Let’s use this as a handy constructor if you already have 
     ’ a feature or want to create one inline. 
     Public Sub New(ByVal feature As Feature) 
         Me.m_feature = feature 
         zoomLevelSet = New ZoomLevelSet() 
  
     End Sub 
  
     ’  This is the feature property 
     Public Property Feature() As Feature 
         Get 
             Return m_feature 
         End Get 
         Set(ByVal value As Feature) 
             m_feature = value 
         End Set 
     End Property 
  
     ’ This is the Zoom Level Set.  This high level object has all of 
     ’ the logic in it for zoom levels, drawing and everything. 
     Public Property ZoomLevels() As ZoomLevelSet 
         Get 
             Return zoomLevelSet 
         End Get 
         Set(ByVal value As ZoomLevelSet) 
             zoomLevelSet = value 
         End Set 
     End Property 
  
  
 End Class 
  
 Class MapShapeLayer 
     Inherits Layer 
     Private m_mapShapes As Dictionary(Of String, MapShape) 
  
     Public Sub New() 
         m_mapShapes = New Dictionary(Of String, MapShape)() 
     End Sub 
  
     ’ Here is where you place all of your map shapes. 
     Public ReadOnly Property MapShapes() As Dictionary(Of String, MapShape) 
         Get 
             Return m_mapShapes 
         End Get 
     End Property 
  
     ’ This is a required overload of the Layer.  As you can see we simply 
     ’ loop through all of our map shapes and then choose the correct zoom level. 
     ’ After that, the zoom level class takes care of the heavy lifiting.  You 
     ’ have to love how easy this framework is to re-use. 
  
     Protected Overloads Overrides Sub DrawCore(ByVal canvas As GeoCanvas, ByVal labelsInAllLayers As Collection(Of SimpleCandidate)) 
         For Each mapShapeKey As String In m_mapShapes.Keys 
             Dim mapShape As MapShape = m_mapShapes(mapShapeKey) 
             Dim currentZoomLevel As ZoomLevel = mapShape.ZoomLevels.GetZoomLevelForDrawing(canvas.CurrentWorldExtent, canvas.Width, GeographyUnit.DecimalDegree) 
             If currentZoomLevel IsNot Nothing Then 
                 If canvas.CurrentWorldExtent.Intersects(mapShape.Feature.GetBoundingBox()) Then 
                     currentZoomLevel.Draw(canvas, New Feature() {mapShape.Feature}, New Collection(Of SimpleCandidate)(), labelsInAllLayers) 
                 End If 
             End If 
         Next 
     End Sub 
  
 End Class

OK I have the multiple icons now showing but cannot figure out how to labels them ?? 
  
 Thanks 
  
  
 Ewan 
  
     Private Sub New_Raster_Position(ByVal Unit_Name As String, ByVal Unit_Operator As String, ByVal Lat As Double, ByVal Lon As Double, ByVal Speed As Double, ByVal Direction As Integer, ByVal Status As String) 
  
         Dim IconPath As String = Get_GIS_Icon(Unit_Name) 
  
         If System.IO.File.Exists(IconPath) = False Then 
             IconPath = AppPath & “\icons\gis\base.png” 
         End If 
  
         gisMAP.Overlays(“iconOverlay”).Lock.EnterWriteLock() 
  
         Try 
             Dim myMapShape As New MapShape(New Feature(Lon, Lat, Unit_Name)) 
  
             myMapShape.ZoomLevels.ZoomLevel01.DefaultPointStyle = New PointStyle(New GeoImage(IconPath)) 
             myMapShape.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20 
  
             If Not iconLayer.MapShapes.ContainsKey(Unit_Name) Then 
                 iconLayer.MapShapes.Add(Unit_Name, myMapShape) 
             Else 
                 iconLayer.Open() 
                 iconLayer.MapShapes.Remove(Unit_Name) 
                 iconLayer.Close() 
                 iconLayer.MapShapes.Add(Unit_Name, myMapShape) 
             End If 
  
         Finally 
             gisMAP.Overlays(“iconOverlay”).Lock.ExitWriteLock() 
         End Try 
  
         gisMAP.Refresh() 
  
     End Sub 
  
 Public Class MapShape 
  
     Private m_feature As Feature 
     Private zoomLevelSet As ZoomLevelSet 
  
     Public Sub New() 
         Me.New(New Feature()) 
     End Sub 
  
     ’ Let’s use this as a handy constructor if you already have 
     ’ a feature or want to create one inline. 
     Public Sub New(ByVal feature As Feature) 
         Me.m_feature = feature 
         zoomLevelSet = New ZoomLevelSet() 
  
     End Sub 
  
     ’  This is the feature property 
     Public Property Feature() As Feature 
         Get 
             Return m_feature 
         End Get 
         Set(ByVal value As Feature) 
             m_feature = value 
         End Set 
     End Property 
  
     ’ This is the Zoom Level Set.  This high level object has all of 
     ’ the logic in it for zoom levels, drawing and everything. 
     Public Property ZoomLevels() As ZoomLevelSet 
         Get 
             Return zoomLevelSet 
         End Get 
         Set(ByVal value As ZoomLevelSet) 
             zoomLevelSet = value 
         End Set 
     End Property 
  
  
 End Class 
  
 Class MapShapeLayer 
     Inherits Layer 
     Private m_mapShapes As Dictionary(Of String, MapShape) 
  
     Public Sub New() 
         m_mapShapes = New Dictionary(Of String, MapShape)() 
     End Sub 
  
     ’ Here is where you place all of your map shapes. 
     Public ReadOnly Property MapShapes() As Dictionary(Of String, MapShape) 
         Get 
             Return m_mapShapes 
         End Get 
     End Property 
  
     ’ This is a required overload of the Layer.  As you can see we simply 
     ’ loop through all of our map shapes and then choose the correct zoom level. 
     ’ After that, the zoom level class takes care of the heavy lifiting.  You 
     ’ have to love how easy this framework is to re-use. 
  
     Protected Overloads Overrides Sub DrawCore(ByVal canvas As GeoCanvas, ByVal labelsInAllLayers As Collection(Of SimpleCandidate)) 
         For Each mapShapeKey As String In m_mapShapes.Keys 
             Dim mapShape As MapShape = m_mapShapes(mapShapeKey) 
             Dim currentZoomLevel As ZoomLevel = mapShape.ZoomLevels.GetZoomLevelForDrawing(canvas.CurrentWorldExtent, canvas.Width, GeographyUnit.DecimalDegree) 
             If currentZoomLevel IsNot Nothing Then 
                 If canvas.CurrentWorldExtent.Intersects(mapShape.Feature.GetBoundingBox()) Then 
                     currentZoomLevel.Draw(canvas, New Feature() {mapShape.Feature}, New Collection(Of SimpleCandidate)(), labelsInAllLayers) 
                 End If 
             End If 
         Next 
     End Sub 
  
 End Class

Ewan,


In order to label the map shape, you have to pay attention following 2 things:
1)      Have to set the TextStyle for the MapShape.
2)      When you add the feature to the MapShape, the corresponding column value has to been added for the target feature.
 
Following is some code snippet to show how to label a text “MapShape1Text” for MapShape1.

Dim mapShapeLayer As MapShapeLayer = New MapShapeLayer()

Dim feature As New Feature(-104, 42)
feature.ColumnValues.Add("MapShapeText", "MapShape1Text")
Dim mapShape1 As New MapShape(feature)
mapShape1.ZoomLevels.ZoomLevel01.DefaultPointStyle = New PointStyle(New GeoImage("..\..\Data\Cargo Plane.png"))
mapShape1.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20
MapShapeLayer.MapShapes.Add("1", mapShape1)
mapShape1.ZoomLevels.ZoomLevel01.DefaultTextStyle = TextStyles.Capital1("MapShapeText")
mapShape1.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20

Let me know if you still have any more questions.


 
Thanks.
 
Yale

This is brilliant  and has enabled me to get the job done with the following : 
  
  
     Private Sub New_Raster_Position(ByVal Unit_Name As String, ByVal Unit_Operator As String, ByVal Lat As Double, ByVal Lon As Double, ByVal Speed As Double, ByVal Direction As Integer, ByVal Status As String) 
  
         Dim IconPath As String = Get_GIS_Icon(Unit_Name) 
  
         If System.IO.File.Exists(IconPath) = False Then 
             IconPath = AppPath & "\icons\gis\base.png" 
         End If 
  
         gisMAP.Overlays("iconOverlay").Lock.EnterWriteLock() 
  
         Try 
             Dim myFeature As New Feature(Lon, Lat, Unit_Name) 
             myFeature.ColumnValues.Add("Unit_Name", Unit_Name & vbCrLf & Format(Now, "hh:mm:ss tt")) 
  
             Dim myMapShape As New MapShape(myFeature) 
  
             myMapShape.ZoomLevels.ZoomLevel01.DefaultTextStyle.TextColumnName = "Unit_Name" 
             myMapShape.ZoomLevels.ZoomLevel01.DefaultTextStyle = TextStyles.CreateMaskTextStyle("Unit_Name", "aerial", 8, DrawingFontStyles.Regular, GeoColor.SimpleColors.Black, GeoColor.SimpleColors.BrightOrange, -30, -40) 
             myMapShape.ZoomLevels.ZoomLevel01.DefaultPointStyle = New PointStyle(New GeoImage(IconPath)) 
             myMapShape.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20 
  
             If Not iconLayer.MapShapes.ContainsKey(Unit_Name) Then 
                 iconLayer.MapShapes.Add(Unit_Name, myMapShape) 
             Else 
                 iconLayer.Open() 
                 iconLayer.MapShapes.Remove(Unit_Name) 
                 iconLayer.Close() 
                 iconLayer.MapShapes.Add(Unit_Name, myMapShape) 
             End If 
  
         Finally 
             gisMAP.Overlays("iconOverlay").Lock.ExitWriteLock() 
         End Try 
  
         gisMAP.Refresh() 
  
     End Sub

Thanks a bunch 
  
  
 Ewan

Ewan, 
  
 Thanks for letting me know your progress. 
  
 Any more questions just let me know. 
  
 Thanks. 
  
 Yale