Android Question [B4X] Google Geocoding REST API Get only City Name [Solved]

Alexander Stolte

Expert
Licensed User
Longtime User
Hello,

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
 

Alexander Stolte

Expert
Licensed User
Longtime User
What is the output of Log(j.GetString) in the two cases?
First is for lanlon: 51.496978759765625,11.968802452087402
B4X:
{
   "plus_code" : {
      "compound_code" : "FXW9+QG Halle (Saale), Germany",
      "global_code" : "9F3HFXW9+QG"
   },
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "11",
               "short_name" : "11",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Adolf-von-Harnack-Straße",
               "short_name" : "Adolf-von-Harnack-Straße",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Stadtbezirk Nord",
               "short_name" : "Stadtbezirk Nord",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Halle (Saale)",
               "short_name" : "Halle (Saale)",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Sachsen-Anhalt",
               "short_name" : "SA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Germany",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "06114",
               "short_name" : "06114",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Adolf-von-Harnack-Straße 11, 06114 Halle (Saale), Germany",
         "geometry" : {
            "location" : {
               "lat" : 51.49673,
               "lng" : 11.96881
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 51.4980789802915,
                  "lng" : 11.9701589802915
               },
               "southwest" : {
                  "lat" : 51.4953810197085,
                  "lng" : 11.9674610197085
               }
            }
         },
         "place_id" : "ChIJVVPu2apkpkcRuPlJ0QUnAL4",
         "plus_code" : {
            "compound_code" : "FXW9+MG Halle (Saale), Germany",
            "global_code" : "9F3HFXW9+MG"
         },
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "20",
               "short_name" : "20",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Adolf-von-Harnack-Straße",
               "short_name" : "Adolf-von-Harnack-Straße",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Stadtbezirk Nord",
               "short_name" : "Stadtbezirk Nord",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Halle (Saale)",
               "short_name" : "Halle (Saale)",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Germany",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "06114",
               "short_name" : "06114",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Adolf-von-Harnack-Straße 20, 06114 Halle (Saale), Germany",
         "geometry" : {
            "location" : {
               "lat" : 51.4965275,
               "lng" : 11.9690773
            },
            "location_type" : "RANGE_INTERPOLATED",
            "viewport" : {
               "northeast" : {
                  "lat" : 51.4978764802915,
                  "lng" : 11.9704262802915
               },
               "southwest" : {
                  "lat" : 51.4951785197085,
                  "lng" : 11.9677283197085
               }
            }
         },
         "place_id" : "Ej5BZG9sZi12b24tSGFybmFjay1TdHJhw59lIDIwLCAwNjExNCBIYWxsZSAoU2FhbGUpLCBEZXV0c2NobGFuZCIaEhgKFAoSCRmD2d2qZKZHEer8MEe8br8xEBQ",
         "types
Message longer than Log limit (4000). Message was truncated.

second is for lanlon: 50.145294189453125,8.388145446777344
B4X:
{
   "plus_code" : {
      "compound_code" : "49WQ+47 Eppstein, Germany",
      "global_code" : "9F2C49WQ+47"
   },
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Eppstein",
               "short_name" : "Eppstein",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Germany",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "65817",
               "short_name" : "65817",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Fritz-Reuter-Str. 89, 65817 Eppstein, Germany",
         "geometry" : {
            "location" : {
               "lat" : 50.14529599999999,
               "lng" : 8.3881459
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 50.1466449802915,
                  "lng" : 8.389494880291501
               },
               "southwest" : {
                  "lat" : 50.1439470197085,
                  "lng" : 8.386796919708496
               }
            }
         },
         "place_id" : "ChIJNxVHkJWkvUcRrMKTINGbzH4",
         "types" : [ "establishment", "point_of_interest" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "11",
               "short_name" : "11",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Eppenhainer Weg",
               "short_name" : "Eppenhainer Weg",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Vockenhausen",
               "short_name" : "Vockenhausen",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Eppstein",
               "short_name" : "Eppstein",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Main-Taunus-Kreis",
               "short_name" : "Main-Taunus-Kreis",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Darmstadt",
               "short_name" : "DA",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Hessen",
               "short_name" : "HE",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Germany",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "65817",
               "short_name" : "65817",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Eppenhainer Weg 11, 65817 Eppstein, Germany",
         "geometry" : {
            "location" : {
               "lat" : 50.14542,
               "lng" : 8.388109999999999
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 50.1467689802915,
                  "lng" : 8.389458980291502
               },
               "southwest" : {
                  "lat" : 50.14407101970851,
                  "lng" : 8.386761019708498
               }
            }
         },
         "place_id" : "ChIJ6xipmpWkvUcRhhoYDVhH3zk",
         "plus_code" : {
            "compound_code" : "49WQ+56 Eppstein, Germany",
            "global_code" : "9F2C49WQ+56"
         },
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "11",
               "short_name" : "11",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Eppenhainer Weg",
               "short_n
Message longer than Log limit (4000). Message was truncated.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip:
This is a mistake:
B4X:
 Dim location As List
                location.Initialize
                location    = first.Get("address_components")
You should never initialize an object and then assign a new object to the same variable replacing the old object. Remove the Initialize line.

You are not parsing the response correctly.
The correct way to do it is by checking the types value.
Something like:
B4X:
Sub GetAddressComponent(Components As List, TargetType As String) As String
 For Each m As Map In Components
  Dim types As List = m.Get("types")
  For Each ttype As String In types
   If ttype = TargetType Then Return m.Get("long_name")
  Next
Next
Log("Not found")
Return ""
End Sub

B4X:
Dim components As List = first.Get("address_components")  
Dim city As String = GetAddressComponent(components, "sublocality_level_1")
 
Upvote 0
Top