Android Question Google map and movable pin

ThePuiu

Active Member
Licensed User
Longtime User
I want to make a window that contains a map in which a pin appears in the current position, but which the user can move in a certain position. When you press a button in the window (below the map) the window will close and I will have access to the coordinates of the pin. How can this be done? Thank you!
 

RichardN

Well-Known Member
Licensed User
Longtime User
Yes you can.

Starting with a normal Google Map (See the tutorial) you employ the GoogleMapsExtras library to define the MarkerOptions which include making the marker draggable by the user.

B4X:
'In Process Globals
Private rp As RuntimePermissions


'In Globals
Private gMap As GoogleMap
Private MapFragment1 As MapFragment        'from designer
Private GMX As GoogleMapsExtras
Private gMarkerDragListener As OnMarkerDragListener
Private HomeMarker As Marker
Private MarkerPosition As LatLng
   
'In code once the layout is loaded

    Wait For MapFragment1_Ready
   
    gMap = MapFragment1.GetMap
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
   
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
   
    If Result Then
        gMap.MyLocationEnabled = True
    Else  
        ToastMessageShow("Map permission refused - Unable to continue",True)
        Activity.Finish
    End If

    MarkerPosition.Initialize(40,50)     'Position 40N 50E

    Dim HomeBitmap As Bitmap = LoadBitmap(File.DirAssets,"home_image.png")
   
    Dim bmd As BitmapDescriptor                'adapt the bitmap for use
    Dim bmf As BitmapDescriptorFactory
    bmd = bmf.FromBitmap(HomeBitmap)
   
    'define the MarkerOptions
   
    Dim mo As MarkerOptions
    mo.Initialize
    mo.Position(MarkerPosition)        'define the position
    mo.Anchor(0.5,0.5)                       'define where the reference position lies within the bitmap
    mo.Icon(bmd)                              'define the icon
    mo.Draggable(True)                     'make it draggable
    mo.Snippet("Target Location")    'add a text snippet if required
   
    HomeMarker = GMX.AddMarker(gMap,mo)
   
    gMarkerDragListener.Initialize("HomeMarker")                             'Setup the listener for marker drag events
    GMX.SetOnMarkerDragListener(gMap,gMarkerDragListener)
   
.........

Sub HomeMarker_DragEnd(Marker1 As Marker)
   
    'Here you can use Marker1.Position properties as you wish
   
End Sub

The purists may not like my use of Activity.Finish but it has the desired effect!
 
Upvote 0

ThePuiu

Active Member
Licensed User
Longtime User
I tried to use the code provided by you together with the example from GoogleMap library. The map opens but although it has checked all the available options (in MapFragment), only the zoom control appears on the screen, without myLocation, and the map does not pan automatically at the current position. Also, the marker does not appear on my map. Do you have any idea what's wrong? I am not receiving any error messages in the application (permission for PERMISSION_ACCESS_FINE_LOCATION is granted).
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
@ThePuiu.... As you have discovered some properties are set in the designer and some in code. There are also some asynchronous problems with Google Maps....

You can't address the map properties until the map is loaded. For instance.... If you try and execute gMap.MyLocationEnabled = True before MapFragment1_Ready then it will throw an exception.

Your own position on the map won't display until Google Maps has a valid GPS position or a coarse position derived from a nearby WiFi or phone mast. If you are testing the device on your desk indoors it may take a short time to figure where it is. If you try and read gMap.Position too early then it will throw an exception. You should always test for that before trying to read it....

B4X:
If gMap.MyLocation.IsInitialized = False Then
        Log("Google Maps does not yet have a valid position")
Else
        'You can now use it
        Dim MyPosition As LatLng = gMap.MyLocation
       
End If

Another very useful facility is being able to suppress the Google Maps toolbar as it is surplus to requirements in most bespoke applications. You do however have to use the JavaObject library to do it.....
B4X:
Wait For MapFragment1_Ready     'you need to wait for the map to load
gMap = Mapfragment1.GetMap
       
Dim jo As JavaObject
jo = gMap.GetUiSettings
jo.RunMethod("setMapToolbarEnabled", Array As Object(False))

I hope this is useful....
 
Upvote 0
Top