B4J Question Google maps marker location

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,


I am trying to port B4A to B4J.
I have 2 movable markers whose positions I need to track.
In B4A the code:
[Private MarkerLocation as LatLng]
MarkerLocation = Marker1.Position
loads MarkerLocation with the LatLng of Marker1.

In B4J Marker.Position is write only.

I can't find anywhere how to get LatLng in B4J

Regards Roger
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Whenever you create a Marker or change its position, add it and its position to a Map:
B4X:
Dim markerMap as Map
markerMap.Initialize
'.... much later
Marker1.Position = MarkerLocation 'set Marker1's new location
markerMap.Put(Marker1, MarkerLocation) 'place them both in a Map so you can retrieve Marker1's location (MarkerLocation) 
'Make sure not to point the MarkerLocation variable to another LatLng object. Create new LatLng variables instead.
'..... much later
Dim someOtherLatLngVariable as LatLng = markerMap.Get(Marker1)   'now you have just retrieved Marker1's location
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks guys,

I will work though it but it doesn't look easy match in to the existing B4A code. [Life wasn't meant to be easy]


B4X:
Sub gmap_Ready
    btnResetMap.Enabled = True
    btnJumpToEiffel.Enabled = True

   Log("map ready")

        'Create BTSMarker Options including ICON
        Private BTSOptions As MarkerOptions
        Private BitmapDescriptor1 As BitmapDescriptor
          Private BitmapDescriptorFactory1 As BitmapDescriptorFactory
        BTSOptions.Initialize
        BTSOptions.Draggable(True) 
        BTSOptions.Position2(BTSLat, BTSLng).Snippet("").Title("BASE")
        BTSOptions.Anchor(0.5, 0.5)
        BitmapDescriptor1=BitmapDescriptorFactory1.FromAsset("markerghost.png")
        BTSOptions.Icon(BitmapDescriptor1)
        'Create BTSmarker
        BTSMarker=GoogleMapsExtras1.AddMarker(gmap, BTSOptions)

        'Create LANDMarker Options including ICON
        Private LANDOptions As MarkerOptions
        Private BitmapDescriptor2 As BitmapDescriptor
          Private BitmapDescriptorFactory2 As BitmapDescriptorFactory
        LANDOptions.Initialize
        LANDOptions.Draggable(True) 
        LANDOptions.Position2(LANDLat, LANDLng).Snippet("").Title("LANDMARK")
        LANDOptions.Anchor(0.5, 0.5)
        BitmapDescriptor2=BitmapDescriptorFactory2.FromAsset("markerghost.png")
        LANDOptions.Icon(BitmapDescriptor2)
        'Creat LANDMarker marker
        LANDMarker =GoogleMapsExtras1.AddMarker(gmap, LANDOptions)
       
        Private AnyDragListener As OnMarkerDragListener
        AnyDragListener.Initialize("AnyDragListener")
        GoogleMapsExtras1.SetOnMarkerDragListener(gmap,AnyDragListener)
       
        HorizonPointLatLng
        DrawLandMarkLine
        DrawAntBearingLine
       
        cp.Initialize(MapLat, MapLng, MapZoom )
        gmap.MoveCamera(cp)
        gmap.maptype = MapTypeInt      'MapTypeInt: 1=Road Map, 2=Satellite, 3=Terrain, 4=Satellite with Labels

End Sub


Sub AnyDragListener_Drag(Marker1 As Marker)
    Projection1=GoogleMapsExtras1.GetProjection(gmap)
   
    MarkerLocation = gmap.get Marker1.Position
    MarkerLocation = Marker1.Position
   
   
    ScreenPosition = Projection1.toScreenLocation(MarkerLocation)
    XX = ScreenPosition.X
    YY = ScreenPosition.Y
    If Marker1 = BTSMarker Then
        'Find Screen X&Y of Marker
        BTSX = XX
         BTSY = YY
        'Move Imageview to Marker Screen position
        BTSIV.Left = BTSX - BTSIV.Width/2
        BTSIV.Top = (BTSY - BTSIV.Height/2) + pnlDispLatLng.Height
       
        Private BTSLatLng As LatLng
        BTSLatLng = BTSMarker.Position
        BTSLat = BTSLatLng.Latitude
        BTSLng = BTSLatLng.Longitude

        Private LandMarkLatLng As LatLng
        LandMarkLatLng = LANDMarker.Position
        LANDLat = LandMarkLatLng.Latitude
        LANDLng = LandMarkLatLng.Longitude
        HorizonPointLatLng
        DrawLandMarkLine
        DrawAntBearingLine
    Else
        LMX = XX
        LMY = YY
        LMIV.Left = LMX - LMIV.Width/2
        LMIV.Top = (LMY - LMIV.Height/2) + pnlDispLatLng.Height
       
        Private BTSLatLng As LatLng
        BTSLatLng = BTSMarker.Position
        BTSLat = BTSLatLng.Latitude
        BTSLng = BTSLatLng.Longitude

        Private LandMarkLatLng As LatLng
        LandMarkLatLng = LANDMarker.Position
        LANDLat = LandMarkLatLng.Latitude
        LANDLng = LandMarkLatLng.Longitude
        DrawLandMarkLine
    End If 
    DispBTSLat.Text = NumberFormat2 (BTSLat, 1 ,  5, 5, False)
    DispBTSLng.Text = NumberFormat2 (BTSLng, 1 , 5, 5, False)
    DispLandLat.Text = NumberFormat2 (LANDLat, 1 , 5, 5, False)
    DispLandLng.Text = NumberFormat2 (LANDLng, 1 , 5, 5, False)
    DispAntBearing.Text = NumberFormat2 (AntBearing, 1 , 1 , 1, False)
End Sub


Regards Roger
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
I don't think B4J Markers are draggable. Or, if the underlying library has that ability, it hasn't been exposed in the B4J library. You'll probably have to rethink that portion of the app.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
All hope is not lost. The GoogleMaps pane is clickable and you can get the LatLng of where the user clicked. It's not quite as neat as dragging, but it still might be useful. Looking at the Javadocs for the underlying library, it doesn't look like it supports Marker dragging.
 
Upvote 0
Top