Hello,
i have this code to get from the lat and lon the city name, streetadress etc.
The Problem is, that: location.Get(3)
The city name is not always in third place, if a coordinate does not have a street name, then the city name is at 1st place for example.
How do I reliably get the city name?
Thanks
i have this code to get from the lat and lon the city name, streetadress etc.
B4X:
Sub PlaceToLatLon(lat As Float, lon As Float) As ResumableSub
Dim j As HttpJob
Dim city2 As String
j.Initialize("", Me)
j.Download2("https://maps.googleapis.com/maps/api/geocode/json", _
Array As String("latlng", lat & "," & lon,"types","(cities)","key",Main.API_KEY))
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim jp As JSONParser
jp.Initialize(j.GetString)
Dim m As Map = jp.NextObject
If m.Get("status") = "OK" Then
Dim results As List = m.Get("results")
If results.Size > 0 Then
Dim first As Map = results.Get(0)
Dim location As List
location.Initialize
location = first.Get("address_components")
Dim city As Map = location.Get(3)
city2 = city.Get("long_name")
'city5 = city.Get("long_name")
End If
End If
Else
Log("Error!")
End If
j.Release
Return city2
End Sub
The Problem is, that: location.Get(3)
The city name is not always in third place, if a coordinate does not have a street name, then the city name is at 1st place for example.
How do I reliably get the city name?
Thanks