I would like to store a Location object and i'm trying to pick a good way to do it. Please give me advice that how to do this
I'm using this code but when i get the location from Preferences then Location return like this...
Location[mProvider=STORAGE,mTime=0,mLatitude=30.0,mLongitude=76.0,mHasAltitude=false,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=false,mAccuracy=0.0,mExtras=null]
/** Store Location object in SharedPreferences */
public void storeLocation(Context context, Location location) {
SharedPreferences settings;
Editor editor;
try {
JSONObject locationJson = new JSONObject();
locationJson.put(LATITUDE, location.getLatitude());
locationJson.put(LONGITUDE, location.getLongitude());
settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
editor = settings.edit();
editor.putString(KEY_LOCATION, locationJson.toString());
editor.commit();
Log.i("location_util_store", "Location" + location);
} catch (Exception e) {
e.printStackTrace();
}
}
/** Retrieve Location object from SharedPreferences
* @return */
public Location getPrevLocation(Context context) {
SharedPreferences settings;
try {
settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
String jsonLocation = settings.getString(KEY_LOCATION, null);
if (jsonLocation != null) {
JSONObject locationJson = new JSONObject(jsonLocation);
Location location = new Location("STORAGE");
location.setLatitude(locationJson.getInt(LATITUDE));
location.setLongitude(locationJson.getInt(LONGITUDE));
Log.i("location_util_get", "Location" + location);
return location;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}