Android Question Lats and Lons From Googlemaps Extra

Terradrones

Active Member
Licensed User
Hi Everybody

I have a whole bunch of coordinates plotted on Google maps. How can I retrieve the Lats and Lons from a circle that I tap on using B4A?

Thank you
Michael
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Full example:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Wait For MapFragment1_Ready
    gmap = MapFragment1.GetMap
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result Or rp.Check(rp.PERMISSION_ACCESS_COARSE_LOCATION) Then
        gmap.MyLocationEnabled = True
    Else
        Log("No permission!")
    End If
    AddCicleListener(gmap) 'call once to enable events.
    
    Dim Circle As JavaObject = AddCircle(gmap, 33, 4, 100000, xui.Color_Red, xui.Color_Red, True)
    Circle.RunMethod("setTag", Array("circle 1"))
End Sub

Private Sub AddCicleListener (Map As GoogleMap)
    Dim event As Object = Map.As(JavaObject).CreateEventFromUI("com.google.android.gms.maps.GoogleMap$OnCircleClickListener", "CircleListener", Null)
    Map.As(JavaObject).RunMethod("setOnCircleClickListener", Array(event))
End Sub

Private Sub CircleListener_Event (MethodName As String, Args() As Object) As Object
    If MethodName = "onCircleClick" Then
        Dim circle As JavaObject = Args(0)
        Log($"circle clicked: ${circle.RunMethod("getTag", Null)}"$)
        Dim center As LatLng = circle.RunMethod("getCenter", Null)
        Log(center)
    End If
    Return Null
End Sub

Private Sub AddCircle (Map As GoogleMap, Latitude As Double, Longitude As Double, RadiusMeters As Double, _
        FillColor As Int, StrokeColor As Int, Clickable As Boolean) As JavaObject
    Dim opt As JavaObject
    opt.InitializeNewInstance("com/google/android/gms/maps/model/CircleOptions".Replace("/", "."), Null)
    Dim ll As LatLng
    ll.Initialize(Latitude, Longitude)
    opt.RunMethod("center", Array(ll))
    opt.RunMethod("radius", Array(RadiusMeters))
    opt.RunMethod("fillColor", Array(FillColor))
    opt.RunMethod("strokeColor", Array(StrokeColor))
    opt.RunMethod("clickable", Array(Clickable))
    Return Map.As(JavaObject).RunMethod("addCircle", Array(opt))
End Sub
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
I am reminded of the saying that "sufficiently complex technology has the appearance of magic".
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
I recently saw this post and realized it was similar to the Google Maps extensions I'd previously posted, but they removed it.

Why?


POST:
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It was posted in the code snippets forum several years ago. It was missing any code or attachment and a user replied to this post asking for the code and it was never answered. I removed it while searching for a solution, as it was only confusing in its current state.
If you still have the code then please post it again.
 
Upvote 0

Terradrones

Active Member
Licensed User
It was posted in the code snippets forum several years ago. It was missing any code or attachment and a user replied to this post asking for the code and it was never answered. I removed it while searching for a solution, as it was only confusing in its current state.
If you still have the code then please post it again.
Erel, the code works very well, even if I do not understand it fully. Thank you very much.

I would appreciate a small change to the coding please. What I have noticed is that if one does not click exactly on the circle, it does not register. Is it please possible to let the user click anywhere on the google map and get the lats and Lons. From there I can calculate to find the closest coordinate point to where the User clicked on the map.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Is it please possible to let the user click anywhere on the google map and get the lats and Lons

Event:
B4X:
Private Sub gmap_Click (Point As LatLng)
    Log(Point.Latitude & "," & Point.Longitude)
End Sub
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
You can add more information about the circle:
Get or Set.
See:

other data.
B4X:
Private Sub CircleListener_Event (MethodName As String, Args() As Object) As Object
    If MethodName = "onCircleClick" Then
        Dim circle As JavaObject = Args(0)
      
        Log($"circle clicked: ${circle.RunMethod("getTag", Null)}"$)
      
        Dim center As LatLng = circle.RunMethod("getCenter", Null)
        Log(center)
      
      
        Dim radius As Double = circle.RunMethod("getRadius", Null) 'radius meters
        Log(radius)
      
        Dim id As String = circle.RunMethod("getId", Null) 'id unique
        Log(id)
      
      
    End If
    Return Null
End Sub
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
From there I can calculate to find the closest coordinate point to where the User clicked on the map.
Do you want to calculate the circle closest to the click made on the map?
 
Upvote 0

Terradrones

Active Member
Licensed User
Do you want to calculate the circle closest to the click made on the map?
Yes, that is the whole idea. Then I can get the lats and longs of the circle, convert it to coordinates and calculate the Bearing and Distance from the Total Station to the selected circle.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Yes, that is the whole idea. Then I can get the lats and longs of the circle, convert it to coordinates and calculate the Bearing and Distance from the Total Station to the selected circle.
You've already done what you're talking about. If not, I have some routines that perform those calculations. Upload an example and I'll help you if you want.
 
Upvote 0

Terradrones

Active Member
Licensed User
You've already done what you're talking about. If not, I have some routines that perform those calculations. Upload an example and I'll help you if you want.
I am not getting any reaction from the following sub:

Private Sub gmap_Click (Point As LatLng)
Log(Point.Latitude & "," & Point.Longitude)
End Sub
 
Upvote 0
Top