So now you will learn how to get Geo-coordinates from strings using Google’s maps API. Since you guys are kind of a newbie you have to learn a little something about the Google maps API and JSON objects. What we will be doing here is we will be accessing Google’s Maps API from our program. The API based on the String query we send will return a JSON object containing the results of our search in it. Pretty straight forward right?
So here is the lowdown on JSON. JSON short for JavaScript Object Notation. It is a standard for information interchange that is easy for humans to read and easy for machines to parse. Hence the use in the Maps API. You can understand the conventions of JSON from the literature here.
So basically what we will be doing here is access the API, send a location to search for in a string, get the JSON object and make the program read and return the Latitude and Longitude from it.
So now that you understand the mechanics of the program Lets dive in. Here is the code and we will dissect it later.
[sourcecode language=”java”]
public String SendLatLong(String Address)
{
String latlong = “”;
try
{
String readadress = readTwitterFeed(“http://maps.googleapis.com/maps/api/geocode/json?address=”+ Address.replaceAll(” “, “%20”) + “&sensor=false”);
JSONObject jsonObject = new JSONObject(readadress).getJSONArray(“results”).getJSONObject(0).getJSONObject(“geometry”).getJSONObject(“location”);
String eventLat = jsonObject.getString(“lat”);
String eventLng = jsonObject.getString(“lng”);
latlong = eventLat + “,” + eventLng;
}
catch (Exception e)
{
}
return latlong;
}
public String readTwitterFeed(String url)
{
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try
{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200)
{
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null)
{
builder.append(line);
}
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return builder.toString();
}
[/sourcecode]
Explanation of program:
The main work being done here is done by the SendLatLong function which is the main function and will be returning the latitude and longitude. Observe that it accepts an argument “Address” which is the name of the location of which we want the latitude and longitude.
Now notice the String variable “latlong”. We will be using this string to return the coordinates that we will be obtaining.
Address(Request) Parsing
Now the core logic of the program is encapsulated in the try-catch block of the function. Let me break this down a notch further. Lets look at the bit of code:
[sourcecode language=”java”]
String readadress = readTwitterFeed(“http://maps.googleapis.com/maps/api/geocode/json?address=”+ Address.replaceAll(” “, “%20”) + “&sensor=false”);
[/sourcecode]
We have another string readadress that is getting a string returned by a function readTwitterFeed. What I want you to closely at is the argument being passed into it. You will notice that our original argument “Address” is concatenated to into a URL. What is happening here is that we are passing queries into Google’s Maps API through a query string. That is why we have to replace the spaces with “%20” so that the search engine can parse it by applying the function replaceAll on Address.
Not getting it? Ok let us get the JSON file for Taj Mahal. As you can see the value for the “Address” is “Taj Mahal”. We will now add it to the URL so we will have to remove the space between Taj and Mahal and put %20 to allow the search engine program to parse it. So our URL will be:
http://maps.googleapis.com/maps/api/geocode/json?address=Taj%20Mahal&sensor=false
Now copy the URL above and paste it into your browser to fetch the JSON object. Give it a shot. Your browser will directly open the JSON file returned by the API.
URL parsing
We do not need to delve deeper into the readTwitterFeed function but you should understand why it is there. What it does is it reads in a string and returns a stringBuilder object. What this does is preventing the data from the JSON file from getting read into the URL which is undesirable because it would result in complicated string parsing methods.
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]
The data that we have stored in the JSON object is the latitude and longitude of our searched location. This data we store into separate strings with the following:
[sourcecode language=”java”]
String eventLat = jsonObject.getString(“lat”);
String eventLng = jsonObject.getString(“lng”);
latlong = eventLat + “,” + eventLng;
[/sourcecode]
The separate strings are concatenated into latlong which is returned by our primary function.
The things you have seen up till now should suffice if you plan to just get the Geo-coordinates . If you want to delve deeper into the details or want to learn how to get other geographic data you will have to read the next post. Till then you can play around with this code and understand it properly. So now you have got a grip on using Google Maps API In Android Application Development. we have used this api in couple of android applications you can check all those android applications on Google store here..