Due to this bug http://code.google.com/p/android/issues/detail?id=8816 google's Geocoder class does not function.
I found ReverseGeocode.java in one of the comments mentioning that it is a work around. i made a method getAddressFromLocation() that will do the job base on this source.
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,4 +9,3 @@
|
|||||||
|
|
||||||
# Project target.
|
# Project target.
|
||||||
target=Google Inc.:Google APIs:9
|
target=Google Inc.:Google APIs:9
|
||||||
proguard.config=proguard.cfg
|
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import android.location.Location;
|
|||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.os.PowerManager.WakeLock;
|
import android.os.PowerManager.WakeLock;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The broadcast receiver that works with LocationService.java
|
* The broadcast receiver that works with LocationService.java
|
||||||
@@ -46,12 +45,14 @@ public class LocationReceiver extends BroadcastReceiver{
|
|||||||
*/
|
*/
|
||||||
private void onLocationUpdate(Location location) {
|
private void onLocationUpdate(Location location) {
|
||||||
//TODO something with the location i.e. report location to social services like twitter, ect...
|
//TODO something with the location i.e. report location to social services like twitter, ect...
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Log.d(TravelPost.TAG, TwitterServices.tweet(mContext, location.getLatitude() +", "+ location.getLongitude() +" ± "+ location.getAccuracy()+"m").toString());
|
Log.d(TravelPost.TAG, TwitterServices.tweet(mContext, ReverseGeocode.getAddressFromLocation(location)).toString());
|
||||||
} catch (TwitterException e) {
|
} catch (TwitterException e) {
|
||||||
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG);
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
removeWakeLock();
|
removeWakeLock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,166 @@
|
|||||||
|
/*
|
||||||
|
Apparently geocoder is not working with the Emulated android. This is a workaround.
|
||||||
|
|
||||||
|
Here is how I implemented it:
|
||||||
|
|
||||||
|
try {
|
||||||
|
possibleAddresses = g.getFromLocation(location.getLatitude(), location.getLongitude(), 3);
|
||||||
|
} catch (IOException e) {
|
||||||
|
if("sdk".equals( Build.PRODUCT )) {
|
||||||
|
Log.d(TAG, "Geocoder doesn't work under emulation.");
|
||||||
|
possibleAddresses = ReverseGeocode.getFromLocation(location.getLatitude(), location.getLongitude(), 3);
|
||||||
|
} else
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.TwentyCodes.android.TravelPost;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import android.location.Address;
|
||||||
|
import android.location.Location;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Due to this bug http://code.google.com/p/android/issues/detail?id=8816 google's Geocoder class does not function.
|
||||||
|
* I found this source in one of the comments mentioning that it is a work around
|
||||||
|
*
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public class ReverseGeocode {
|
||||||
|
|
||||||
|
public static List<Address> getFromLocation(double lat, double lon, int maxResults) {
|
||||||
|
String urlStr = "http://maps.google.com/maps/geo?q=" + lat + "," + lon + "&output=json&sensor=false";
|
||||||
|
String response = "";
|
||||||
|
List<Address> results = new ArrayList<Address>();
|
||||||
|
HttpClient client = new DefaultHttpClient();
|
||||||
|
|
||||||
|
Log.d("ReverseGeocode", urlStr);
|
||||||
|
try {
|
||||||
|
HttpResponse hr = client.execute(new HttpGet(urlStr));
|
||||||
|
HttpEntity entity = hr.getEntity();
|
||||||
|
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
|
||||||
|
|
||||||
|
String buff = null;
|
||||||
|
while ((buff = br.readLine()) != null)
|
||||||
|
response += buff;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONArray responseArray = null;
|
||||||
|
try {
|
||||||
|
JSONObject jsonObject = new JSONObject(response);
|
||||||
|
responseArray = jsonObject.getJSONArray("Placemark");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("ReverseGeocode", "" + responseArray.length() + " result(s)");
|
||||||
|
|
||||||
|
for(int i = 0; i < responseArray.length() && i < maxResults-1; i++) {
|
||||||
|
Address addy = new Address(Locale.getDefault());
|
||||||
|
|
||||||
|
try {
|
||||||
|
JSONObject jsl = responseArray.getJSONObject(i);
|
||||||
|
|
||||||
|
String addressLine = jsl.getString("address");
|
||||||
|
|
||||||
|
if(addressLine.contains(","))
|
||||||
|
addressLine = addressLine.split(",")[0];
|
||||||
|
|
||||||
|
addy.setAddressLine(0, addressLine);
|
||||||
|
|
||||||
|
jsl = jsl.getJSONObject("AddressDetails").getJSONObject("Country");
|
||||||
|
addy.setCountryName(jsl.getString("CountryName"));
|
||||||
|
addy.setCountryCode(jsl.getString("CountryNameCode"));
|
||||||
|
|
||||||
|
jsl = jsl.getJSONObject("AdministrativeArea");
|
||||||
|
addy.setAdminArea(jsl.getString("AdministrativeAreaName"));
|
||||||
|
|
||||||
|
jsl = jsl.getJSONObject("SubAdministrativeArea");
|
||||||
|
addy.setSubAdminArea(jsl.getString("SubAdministrativeAreaName"));
|
||||||
|
|
||||||
|
jsl = jsl.getJSONObject("Locality");
|
||||||
|
addy.setLocality(jsl.getString("LocalityName"));
|
||||||
|
|
||||||
|
addy.setPostalCode(jsl.getJSONObject("PostalCode").getString("PostalCodeNumber"));
|
||||||
|
addy.setThoroughfare(jsl.getJSONObject("Thoroughfare").getString("ThoroughfareName"));
|
||||||
|
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
results.add(addy);
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the hacked up method from above the will only return the string address of the first search result
|
||||||
|
* @param lat
|
||||||
|
* @param lon
|
||||||
|
* @return string address, or lat, lon if search fails
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public static String getAddressFromLocation(Location location) {
|
||||||
|
String urlStr = "http://maps.google.com/maps/geo?q=" + location.getLatitude() + "," + location.getLongitude() + "&output=json&sensor=false";
|
||||||
|
String response = "";
|
||||||
|
HttpClient client = new DefaultHttpClient();
|
||||||
|
|
||||||
|
Log.d("ReverseGeocode", urlStr);
|
||||||
|
try {
|
||||||
|
HttpResponse hr = client.execute(new HttpGet(urlStr));
|
||||||
|
HttpEntity entity = hr.getEntity();
|
||||||
|
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
|
||||||
|
|
||||||
|
String buff = null;
|
||||||
|
while ((buff = br.readLine()) != null)
|
||||||
|
response += buff;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("ReverseGeocode", response);
|
||||||
|
|
||||||
|
|
||||||
|
JSONArray responseArray = null;
|
||||||
|
try {
|
||||||
|
JSONObject jsonObject = new JSONObject(response);
|
||||||
|
responseArray = jsonObject.getJSONArray("Placemark");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
return location.getLatitude() +", "+ location.getLongitude() +" ± "+ location.getAccuracy()+"m";
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("ReverseGeocode", "" + responseArray.length() + " result(s)");
|
||||||
|
|
||||||
|
try {
|
||||||
|
JSONObject jsl = responseArray.getJSONObject(0);
|
||||||
|
return jsl.getString("address");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return location.getLatitude() +", "+ location.getLongitude() +" ± "+ location.getAccuracy()+"m";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -42,8 +42,7 @@ public class TwitterServices {
|
|||||||
*/
|
*/
|
||||||
public static Status tweet(Context context, String msg) throws TwitterException{
|
public static Status tweet(Context context, String msg) throws TwitterException{
|
||||||
shared_prefs = context.getSharedPreferences(TravelPost.SETTINGS, 0);
|
shared_prefs = context.getSharedPreferences(TravelPost.SETTINGS, 0);
|
||||||
// Log.d(TravelPost.TAG, shared_prefs.getString(TWITTER_AUTH_TOKEN, ""));
|
Log.d(TravelPost.TAG, "Tweeting: "+ msg);
|
||||||
// Log.d(TravelPost.TAG, shared_prefs.getString(TWITTER_AUTH_SECRET, ""));
|
|
||||||
ConfigurationBuilder cb = new ConfigurationBuilder();
|
ConfigurationBuilder cb = new ConfigurationBuilder();
|
||||||
cb.setDebugEnabled(true);
|
cb.setDebugEnabled(true);
|
||||||
cb.setOAuthConsumerKey(TC_TEST_OAUTH_KEY);
|
cb.setOAuthConsumerKey(TC_TEST_OAUTH_KEY);
|
||||||
|
|||||||
Reference in New Issue
Block a user