Today I did some research for my Where Am I? app, and didn't find much, until I eventually discovered the Google Geocoding API, which is exactly what I was looking for! Not only did it provide looking up areas' coordinates, but it also did something called Reverse Geocoding, which is essentially what I needed. What reverse geocoding is, is when you're given latitude and longitude, and you need to figure out a location, such as a nearby address and zipcode. I figured out that the Reverse Geocoding via the Google Geocoding API allows me to use JSON to figure out a location from the coordinates the tablet gives me, which not only lets me look up the location for my app, but also ties into my last app (the JSON Example), and I can use some code from that.
I copied over my two JSON read methods that I found earlier in a StackOverflow post (from the past project).
public static JSONObject readJsonFromUrl(String url) throws Exception {
InputStream inputStream = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
String jsonText = read(rd);
JSONObject json = new JSONObject(jsonText);
return json;
}
finally {
inputStream.close();
}
}
private static String read(Reader rd) throws Exception {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
The Google Geocoding API requires an API key. This is limited due to Google wanting to limit a certain amount of requests per day (so that they don't get overloaded by them). Luckily, I have a Google Developer License, so I was able to easily obtain one that allows me a very decent amount (in the low thousands) of requests per day. I haven't tested the app yet to figure out which JSON arrays I need, etc., but I'm very sure that I'm extremely close to finishing the app. Not only will it tell you the nearest address, but it will also tell you your city, state, postal code, country, and even where you're standing (whether it be on a rooftop, etc.). I haven't yet tested the accuracy of this, but it seems to me like it'll be very accurate!
This is a wonderful discovery, but is not what you need for the "Where Am I?" app. I just want to be able to read a geolocation from the GPS equipped device. I'm not interested in translating a know address into a longitude and latitude, but rather in just reading the location data automatically generated by the GPS receiver in the phone and displaying it in a friendly manner to the user.
ReplyDelete