Reputation: 5639
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"));
}
}
Upvotes: 1
Views: 2025
Reputation: 5639
Okay so I managed to do it using shared preferences, the code is a little messy and I dont know how inefficient it is for the operation of the device but here goes
To store the multiple routes i use these 2 classes
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);
SharedPreferences numberOfRoutes = context.getSharedPreferences("NUM_ROUTES", context.MODE_PRIVATE);
Editor editor = storeAllRoutes.edit();
Editor numEdit = numberOfRoutes.edit();
i = numberOfRoutes.getInt("numOfRoutes",0);
editor.putString("saveData"+i,saveData.toString());
i++;
numEdit.putInt("numOfRoutes",i);
editor.commit();
numEdit.commit();
Toast.makeText(getApplicationContext(), "Journey Saved",
Toast.LENGTH_SHORT).show();
}
Then to retrieve the data I used
public JSONArray getData(Context context, int i) throws Exception {
SharedPreferences storeAllRoutes = context.getSharedPreferences("STORED_ROUTES", context.MODE_PRIVATE);
SharedPreferences numberOfRoutes = context.getSharedPreferences("NUM_ROUTES", context.MODE_PRIVATE);
if (numberOfRoutes.contains("numOfRoutes")&&i>0) {
return new JSONArray(storeAllRoutes.getString("saveData" + i, ""));
} 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"));
}
}
I call the retrieve methods in the onCreate method using:
SharedPreferences numberOfRoutes = this.getSharedPreferences("NUM_ROUTES", this.MODE_PRIVATE);
try {
if (numberOfRoutes.contains("numOfRoutes")&&numberOfRoutes.getInt("numOfRoutes",0)>0) {
for(int i = 1;i<numberOfRoutes.getInt("numOfRoutes",0)+1;i++ )
{
allRoutes = getData(this,i);
parseJSONArray(allRoutes);
}
}
else{
Toast.makeText(getApplicationContext(), "No routes exist",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 1
Reputation: 2409
Why do you want it in shared preferences? Why not save it as a (list of) serialized objects, as described in this answer. I believe latlng is not serializable (as it is final), but you can fix it like this similar case:
public class TaxiRouteData implements Serializable{
private double startlat;
private double startlong;
private double endlat;
private double endlong;
public TaxiRouteData() {
}
public TaxiRouteData(LatLng startlocation, LatLng endlocation) {
this.startlat = startlocation.latitude;
this.startlong = startlocation.longitude;
this.endlat = endlocation.latitude;
this.endlong = endlocation.longitude; }
public LatLng getStartlocation() {
return new LatLng(startlat,startlong);
}
public void setStartlocation(LatLng startlocation) {
this.startlat = startlocation.latitude;
this.startlong = startlocation.longitude;
}
public LatLng getEndlocation() {
return new LatLng(endlat,endlong);
}
public void setEndlocation(LatLng endlocation) {
this.endlat = endlocation.latitude;
this.endlong = endlocation.longitude; }
This class is serializable and holds some (in this case only 2, start and end) LatLng points, in a similar setup you can use it to serialize and save arrays or lists of LatLng,
Upvotes: 1
Reputation: 104
You could also make use of JSON format. Simply parse your collection into JSON formatted string, save this string in preferences.
But generaly speaking I would suggest using SQLIte aswell. Would be easier to manage your colelction for you.
Upvotes: 0
Reputation: 3
Instead of using preferences you can use Sqlite database. But if you insist on using SharedPreferences then I don't think you have much choice as you can only store boolean, float, int, long, String or StringSet. The only possible way I see is that you concatenate all of the values with your own separator.
Upvotes: 0