【发布时间】:2015-07-19 07:02:57
【问题描述】:
目前我有一个应用程序可以跟踪用户位置并使用折线和地图标记绘制路线,我需要将包含 LatLng 坐标的数组列表添加到另一个存储所有路线的数组列表中, 即 latlng 数组列表是一条路线,因此我需要将所有路线存储在一个数组列表中并将其存储在共享首选项中,以便我可以将用户采取的所有路线加载到地图中。 到目前为止,我只存储了一条到共享首选项的路线,并在每次添加新路线时覆盖它。
这是我目前所拥有的,这 2 个类保存了 latlng 数据
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();
}
这两个类检索数据
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"));
}
}
【问题讨论】:
-
@Kunu 该答案描述了如何保存单个数组列表,我正在考虑保存多个可以检索的数组
-
您可以根据需要使用一些特殊符号使用字符串连接,并在需要时提取它。
标签: android google-maps arraylist