Currently I have an app that tracks user location and draws the route by using polylines and map markers, I need to add the arraylist that contains the LatLng coordinates to another array list that stores all routes, i.e the latlng arraylist is one route, so i need to store all routes in one arraylist and store that in shared preferences so i can load all the routes the user has taken to a map. What I have so far stores just one route to the shared preferences and overwrites it every time a new route is added.
Here is what i have so far, these 2 classes save the latlng data
public void addToJSONArray(JSONArray array, LatLng item) throws Exception {
JSONObject data = new JSONObject();
data.put("latitude", item.latitude);
data.put("longitude", item.longitude);
array.put(data);
}
public void saveJourney(Context context, JSONArray saveData) {
SharedPreferences storeAllRoutes = context.getSharedPreferences("STORED_ROUTES", context.MODE_PRIVATE);
Editor editor = storeAllRoutes.edit();
editor.putString("saveData",saveData.toString());
editor.commit();
}
and these 2 classes retrieve the data
public JSONArray getData(Context context) throws Exception{
SharedPreferences storeAllRoutes = context.getSharedPreferences("STORED_ROUTES", context.MODE_PRIVATE);
if(storeAllRoutes.contains("saveData")) {
return new JSONArray(storeAllRoutes.getString("saveData", ""));
} else {
return new JSONArray();
}
}
public void parseJSONArray(JSONArray array) throws Exception {
for(int i = 0; i < array.length(); ++i) {
JSONObject item1 = array.getJSONObject(i);
LatLng location = new LatLng(item1.getDouble("latitude"), item1.getDouble("longitude"));
mMap.addMarker(new MarkerOptions().position(location).title("Start"));
}
}