Working with the Google Maps JSON file

Share

This is a continuation from our previous post on working with Google Maps API. This post expands on the JSON file returned by the Google Maps API. To understand the conventions of the JSON file let me refer you to conventions used in JSON in this link here. Note that we were retrieving the latitude and longitude of a location. After understanding this post you can modify the code to get other kinds of data from the file. Now moving on we now have a static web address pointing to a JSON resource that is stored in the string readadress. We read the same address into a JSON object as shown below: [sourcecode language=”java”] JSONObject jsonObject = new JSONObject(readadress).getJSONArray(“results”).getJSONObject(0).getJSONObject(“geometry”).getJSONObject(“location”); [/sourcecode] Now you might be wondering here :”Ok we are passing the location of the JSON file through the string readadress to the JSONObject constructor but what the hell are the other things going on about?”. To properly understand what each breadcrumb is accessing I will colour code each breadcrumb and you can match the colour to a corresponding resource in the JSON file below it.

Track the breadcrumbs in the thumbnail of the JSON file below.

Let me show you the contents of the JSON file that is returned for our “Taj Mahal” query now: If you trace it properly you see that in the end you have initialized a JSONObject with 2 attributes that are copied from the location object. In other words you finally have the coordinates which you then copy into the latlong string (that will be returned by the main function SendLatLong) with the following code. [sourcecode language=”java”] String eventLat = jsonObject.getString(“lat”); String eventLng = jsonObject.getString(“lng”); latlong = eventLat + “,” + eventLng; [/sourcecode] Alternatively you can modify the code to fetch different details like PIN codes and state and country. Just trace your breadcrumb to the proper object in the JSON file and copy it to proper objects. Now that you have learned this go and show people some awesomeness, tell us if you need any help.We hope that this post will help you to build crazy & amazing mobile application.