Android Question Parsing JSON Data (Google Maps Geocoding results)

Mike Webster

Member
Licensed User
I am banging my head into my desk trying to figure out how to parse the results from Google Maps to a geocoding request. I need to ultimately grab the location latitude and longitude from this but I am not having much luck. Below is a sample of the info returned by Google. If anyone could provide some guidance, I would appreciate it.

{
"results" : [
{
"address_components" : [
{
"long_name" : "11320",
"short_name" : "11320",
"types" : [ "street_number" ]
},
{
"long_name" : "Pegasus Street",
"short_name" : "Pegasus St",
"types" : [ "route" ]
},
{
"long_name" : "Highland Meadows",
"short_name" : "Highland Meadows",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Dallas",
"short_name" : "Dallas",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Dallas County",
"short_name" : "Dallas County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Texas",
"short_name" : "TX",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "75238",
"short_name" : "75238",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "11320 Pegasus St, Dallas, TX 75238, USA",
"geometry" : {
"location" : {
"lat" : 32.8747287,
"lng" : -96.6859621
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 32.8760776802915,
"lng" : -96.6846131197085
},
"southwest" : {
"lat" : 32.8733797197085,
"lng" : -96.68731108029151
}
}
},
"place_id" : "EicxMTMyMCBQZWdhc3VzIFN0LCBEYWxsYXMsIFRYIDc1MjM4LCBVU0EiGxIZChQKEgmzlF5j6qBOhhET8EBdGhaHrxC4WA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
 

TILogistic

Expert
Licensed User
Longtime User
?
B4X:
    Dim parser As JSONParser
    parser.Initialize(Text)
    Dim mRoot As Map = parser.NextObject

    Dim status As String = mRoot.Get("status")
    Log(status)
  
    For Each colresults As Map In mRoot.Get("results").As(List)

        'Option 1
        Dim geometry As Map = colresults.Get("geometry")
        Dim location As Map = geometry.Get("location")
        Dim lat As Double = location.Get("lat")
        Dim lng As Double = location.Get("lng")
        Log($"${lat} / ${lng}"$)
      
        'Option 2
        Dim lat As Double = colresults.Get("geometry").As(Map).Get("location").As(Map).Get("lat")
        Dim lng As Double = colresults.Get("geometry").As(Map).Get("location").As(Map).Get("lng")
        Log($"${lat} / ${lng}"$)
    Next

1636930651018.png
 
Upvote 1

TILogistic

Expert
Licensed User
Longtime User
less code:
B4X:
    Dim mRoot As Map = Text.As(JSON).ToMap 'ignore
    Dim status As String = mRoot.Get("status")
    Log(status)
  
    For Each colresults As Map In mRoot.Get("results").As(List)
        Dim lat As Double = colresults.Get("geometry").As(Map).Get("location").As(Map).Get("lat")
        Dim lng As Double = colresults.Get("geometry").As(Map).Get("location").As(Map).Get("lng")
        Log($"${lat} / ${lng}"$)
    Next
1636931939055.png
 
Last edited:
Upvote 1

emexes

Expert
Licensed User
@oparra's solution is heaps better, but here's another approach { just in case | out of interest }:

regex for latitude = /"location".*?"lat"\s*:\s*(\-?\d*.?\d*)/gs

and similarly for longitude = /"location".*?"lng"\s*:\s*(\-?\d*.?\d*)/gs

1636936704546.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
direct reading
B4X:
    Dim mRoot As Map = Text.As(JSON).ToMap 'ignore
    Dim lat As Double = mRoot.Get("results").As(List).Get(0).As(Map).Get("geometry").As(Map).Get("location").As(Map).Get("lat")
    Dim lng As Double = mRoot.Get("results").As(List).Get(0).As(Map).Get("geometry").As(Map).Get("location").As(Map).Get("lng")

    Log($"${lat},${lng}"$)
1636937500773.png
 
Upvote 1

emexes

Expert
Licensed User
B4X:
    Dim mRoot As Map = Text.As(JSON).ToMap 'ignore
    Dim lat As Double = mRoot.Get("results").As(List).Get(0).As(Map).Get("geometry").As(Map).Get("location").As(Map).Get("lat")
    Dim lng As Double = mRoot.Get("results").As(List).Get(0).As(Map).Get("geometry").As(Map).Get("location").As(Map).Get("lng")

That looks a bit verbose by your standards ? can you move common parts of the bottom two lines up into the top line?
 
Last edited:
Upvote 0
Top