B4J Question B4J Question Geocoder

jmon

Well-Known Member
Licensed User
Longtime User
I have the code already done, this one is using HttpUtils, Json and Stringutils, for Google Maps API:

B4X:
Private Sub ReverseGeocode(Lat as Double, Lng As Double)
    Dim Key As String = "12234569787"
    Dim job As HttpJob
    job.Initialize("reversegeocode", Me)
    Dim su As StringUtils
    Dim postString As String
    postString = "latlng=" & su.EncodeUrl($"${lat},${lng}"$, "UTF8") _
        & "&key=" & Key
    job.PostString("https://maps.googleapis.com/maps/api/geocode/json?" & postString, "")
End Sub

Private Sub JobDone (Job As HttpJob)
   If Job.Success Then 
        Select Job.JobName
            Case "reversegeocode"
                Dim Json As JSONParser
                Json.Initialize(Job.GetString)
                Dim m As Map = Json.NextObject
                If m <> Null And m.IsInitialized Then
                    Select m.GetDefault("status", "")
                        Case "OK"
                            Dim Results As List = m.GetDefault("results", Null)
                            If Results <> Null And Results.IsInitialized Then
                                For Each Result As Map In Results

                                    Dim FormattedAddress As String = Result.GetDefault("formatted_address", "")                               
                                    Dim Geometry As Map = Result.GetDefault("geometry", Null)
                                    Dim Location As Map = Geometry.GetDefault("location", Null)
                                                  
                                Next
                            End If 

                        Case "ZERO_RESULTS"
                            LogDebug($"No Results"$)
                         
                        Case Else
                            LogDebug($"Error:${Job.GetString}"$)
                            LogDebug($"No Results"$)
                                                                            
                    End Select
                End If
        End Select
    Else
        If Job <> Null And Job.IsInitialized Then
            LogDebug($"Error:${Job.ErrorMessage}"$)
            Select True
                Case Job.ErrorMessage.Contains("java.net.UnknownHostException"), _
                        Job.ErrorMessage.Contains("java.net.SocketException: Network is unreachable")
                    LogDebug("Error")
            End Select      
        End If
    End If 
End Sub
 
Upvote 0
Top