Android Question Drawing fixed lines on an OSMDroid Mapview

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I want center markings on a OSMDroid Mapview, so a vertical line at the horizontal middle of the Mapview and a vertical line on the vertical middle of the Mapview, so a cross spanning the width and height of the Mapview, indicating the center of the Mapview.
I am currently doing this by drawing on a pathoverlay. This works ok, but of course I need to redraw every time the map center changes.
Is there any way to do this drawing on a view that doesn't move with the map, so I have to draw only once?
There is Mapview1.Background, but not sure how to draw on that.
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I want center markings on a OSMDroid Mapview, so a vertical line at the horizontal middle of the Mapview and a vertical line on the vertical middle of the Mapview, so a cross spanning the width and height of the Mapview, indicating the center of the Mapview.
I am currently doing this by drawing on a pathoverlay. This works ok, but of course I need to redraw every time the map center changes.
Is there any way to do this drawing on a view that doesn't move with the map, so I have to draw only once?
There is Mapview1.Background, but not sure how to draw on that.
Still not figured this out.

I tried this:

B4X:
MapView1.Background = MakeCenterMarkersBitMap(MapView1.Width, MapView1.Height, Colors.Red, 1)
MapView1.Invalidate

Sub MakeCenterMarkersBitMapDrawable(iWidth As Int, iHeight As Int, iColour As Int, fStrokeWidth As Float) As BitmapDrawable
 
    Dim bmp As Bitmap
    Dim bmpd As BitmapDrawable
    Dim cvs As Canvas
     
    bmp.InitializeMutable(iWidth * 100dip / 100, iHeight * 100dip / 100)
    cvs.Initialize2(bmp)
    '            x1 y1         x2      y2
    cvs.DrawLine(0, iHeight/2, iWidth, iHeight/2, iColour, fStrokeWidth) 'horizontal line
    cvs.DrawLine(iWidth/2, 0, iWidth/2, iHeight, iColour, fStrokeWidth)  'vertical line
    
    bmpd.Initialize(cvs.Bitmap)
    
    Return bmpd
 
End Sub

But no centermarker lines show

Also tried:

B4X:
MapView1.SetBackgroundImage(MakeCenterMarkersBitMap(MapView1.Width, MapView1.Height, Colors.Red, 1))
MapView1.Invalidate

Sub MakeCenterMarkersBitMap(iWidth As Int, iHeight As Int, iColour As Int, fStrokeWidth As Float) As Bitmap
 
    Dim bmp As Bitmap
    Dim cvs As Canvas
     
    bmp.InitializeMutable(iWidth * 100dip / 100, iHeight * 100dip / 100)
    cvs.Initialize2(bmp)
    '            x1 y1         x2      y2
    cvs.DrawLine(0, iHeight/2, iWidth, iHeight/2, iColour, fStrokeWidth) 'horizontal line
    cvs.DrawLine(iWidth/2, 0, iWidth/2, iHeight, iColour, fStrokeWidth)  'vertical line

    Return cvs.Bitmap
 
End Sub

But again no lines show.
Any suggestion how this can be done?

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Still not figured this out.

I tried this:

B4X:
MapView1.Background = MakeCenterMarkersBitMap(MapView1.Width, MapView1.Height, Colors.Red, 1)
MapView1.Invalidate

Sub MakeCenterMarkersBitMapDrawable(iWidth As Int, iHeight As Int, iColour As Int, fStrokeWidth As Float) As BitmapDrawable
 
    Dim bmp As Bitmap
    Dim bmpd As BitmapDrawable
    Dim cvs As Canvas
    
    bmp.InitializeMutable(iWidth * 100dip / 100, iHeight * 100dip / 100)
    cvs.Initialize2(bmp)
    '            x1 y1         x2      y2
    cvs.DrawLine(0, iHeight/2, iWidth, iHeight/2, iColour, fStrokeWidth) 'horizontal line
    cvs.DrawLine(iWidth/2, 0, iWidth/2, iHeight, iColour, fStrokeWidth)  'vertical line
   
    bmpd.Initialize(cvs.Bitmap)
   
    Return bmpd
 
End Sub

But no centermarker lines show

Also tried:

B4X:
MapView1.SetBackgroundImage(MakeCenterMarkersBitMap(MapView1.Width, MapView1.Height, Colors.Red, 1))
MapView1.Invalidate

Sub MakeCenterMarkersBitMap(iWidth As Int, iHeight As Int, iColour As Int, fStrokeWidth As Float) As Bitmap
 
    Dim bmp As Bitmap
    Dim cvs As Canvas
    
    bmp.InitializeMutable(iWidth * 100dip / 100, iHeight * 100dip / 100)
    cvs.Initialize2(bmp)
    '            x1 y1         x2      y2
    cvs.DrawLine(0, iHeight/2, iWidth, iHeight/2, iColour, fStrokeWidth) 'horizontal line
    cvs.DrawLine(iWidth/2, 0, iWidth/2, iHeight, iColour, fStrokeWidth)  'vertical line

    Return cvs.Bitmap
 
End Sub

But again no lines show.
Any suggestion how this can be done?

RBS
Done this now with an extra panel on top of the Mapview.
All simple, but was trying to avoid this in case it interfered with the Mapview layers.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Done this now with an extra panel on top of the Mapview.
All simple, but was trying to avoid this in case it interfered with the Mapview layers.

RBS
Just for completeness:

B4X:
Sub DrawMapCenterLines(iColour As Int, fStrokeWidth As Float)
    
    Dim cvs As B4XCanvas
    Dim Rect As B4XRect
    
    If iColour = iCenterLinesColour Then Return
    
    Rect.Initialize(0, 0, pnlCenterMarkings.Width, pnlCenterMarkings.Height)
    cvs.Initialize(pnlCenterMarkings)
    cvs.ClearRect(Rect) 'clear any previous lines (pharmacy lines are green)
    cvs.Invalidate

    cvs.DrawLine(0, pnlCenterMarkings.Height/2, pnlCenterMarkings.Width, pnlCenterMarkings.Height/2, iColour, fStrokeWidth)
    cvs.DrawLine(pnlCenterMarkings.Width/2, 0, pnlCenterMarkings.Width/2, pnlCenterMarkings.Height, iColour, fStrokeWidth)
    cvs.Invalidate
    
    iCenterLinesColour = iColour
    
End Sub

RBS
 
Upvote 0
Top