Android Code Snippet Identify Lat Lon geodata by address

Hello friends
I would like to share with you my code that I created this afternoon to find the geodata based on an address.
I hope you can do something with it.
Maybe you can extend it and create a list for the multiple entries with selection and post it here if you want.

B4X:
Sub Geodaten
    
Dim HttpJobGPS As HttpJob
HttpJobGPS.Initialize("GPS_Daten", Me)

Dim url,Stadt,Platz,Strasse,Nummer As String
'-----------------------------------------------------
    Stadt = "33602 Bielefeld"
    Platz = ""
    Strasse = "Jahnplatz"
    Nummer = "10"
'----------------------------------------------------- 
url = "https://nominatim.openstreetmap.org/search?format=json&limit=10&addressdetails=1"
url = url & "&q=" & Platz & "%20" & Stadt & "%20" & Nummer & "%20" & Strasse
HttpJobGPS.download(url)
    
End Sub

Sub JobDone(Job As HttpJob)
    
If Job.Success = True Then
 Dim res As String
 res = Job.GetString
 
Dim parser As JSONParser
parser.Initialize(res)
 Log("Parser " & res)
End If

Dim root As List = parser.NextArray
For Each colroot As Map In root
'-----------------------------------------------------
 Dim boundingbox As List = colroot.Get("boundingbox")
 Log(boundingbox.Get(0))
 Log(boundingbox.Get(2))
 Log(boundingbox.Get(1))
 Log(boundingbox.Get(3))
'----------------------------------------------------- 
 Dim address As Map = colroot.Get("address")
 Log(address.Get("country"))
 Log(address.Get("country_code"))
 Log(address.Get("town"))
 Log(address.Get("road"))
 Log(address.Get("neighbourhood"))
 Log(address.Get("county"))
 Log(address.Get("postcode"))
 Log(address.Get("suburb"))
 Log(address.Get("house_number"))
 Log(address.Get("state"))
'-----------------------------------------------------
 Log(colroot.Get("importance"))
 Log(colroot.Get("display_name"))
 Log(colroot.Get("type"))
 Log(colroot.Get("class"))
 Log(colroot.Get("place_id"))
 Log(colroot.Get("lat"))
 Log(colroot.Get("lon"))
 
Next
 
End Sub
 

Omar Parra A.

Expert
Licensed User
Longtime User
1. You should never have a JobDone sub. https://www.b4x.com/search?query=Wait+For+OkHttp
2. You should use HttpJob.Download2 to correctly encode the parameters.
3. Don't forget to call Job.Release.
4. Your code will fail if the request failed (which should be expected to happen from time to time).

???

B4X:
    Wait For (GeoCoderNominatim(Search)) Complete (Result As String)
    
    TextArea1.Text = JsonPretty(Result)

B4X:
Sub GeoCoderNominatim(Query As String) As ResumableSub
    Dim ResultURL As String
    Dim j As HttpJob
    Dim Parameter() As String = Array As String ("q", Query, "format", "json", "limit", "1", "addressdetails", "1")
    Try
        j.Initialize("", Me)
        j.Download2("https://nominatim.openstreetmap.org/search", Parameter)
        j.GetRequest.SetHeader("Content-Type","application/json")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            ResultURL = j.GetString
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return ResultURL
End Sub

Enter address get location

1624171594261.png
1624171766057.png


Enter location get address

1624171903334.png
1624172208716.png
 

Omar Parra A.

Expert
Licensed User
Longtime User
See:
 
Top