How to save additional Google map overlays in Android app?

I created an application in which users can add various markers to Google maps using overlays. I want the application to save the overlay when the user creates it so that the markers are still present when the application is reopened.
Currently, when you reopen the application, the created tokens have disappeared. I searched the Internet and did not get a clear idea of ​​how to keep my markers / overlays offline for later use.

+5
source share
4 answers

As already mentioned, you need to use some persistent storage . Perhaps the database will be redundant in your particular case (if you just need to save a couple of long-latitude pairs), and the general settings will meet your needs. If you rarely need to read / store this data, then placing JSONArrayas a String for general settings will be the easiest (in terms of reading and retrieving data) solution:

    SharedPreferences.Editor editor = getSharedPreferences("name", 0).edit();
    JSONArray jsonArr = new JSONArray();
    JSONObject json;
    for (int i = 0; i < user_markers.size(); i++) { // here I assume that user_markers is a list where you store markers added by user
        json = new JSONObject();
        json.put("lat", user_markers.get(i).getLat());
        json.put("long", user_markers.get(i).getLong());
        jsonArr.put(json);
    }
    editor.putString("markers", jsonArr.toString());
    editor.commit();

If you need to read / store this data more often, you can assign identifiers / indices to separate SharedPreferences, but this may require a more complex extraction method. For instance:

    SharedPreferences.Editor editor = getSharedPreferences("name", 0).edit();
    int id;
    for (int i = 0; i < user_markers.size(); i++) { // here I assume that user_markers is a list where you store markers added by user
        id = user_markers.get(i).getId(); // some Id here
        // or simply id = i;
        editor.putString("markers_lat_" + id, user_markers.get(i).getLat());
        editor.putString("markers_long_" + id, user_markers.get(i).getLong());
    }
    editor.commit();

In the end, you should consider using a database if you plan to store a large amount of data or data of a complex structure.

+3

, ...

+2

. SQLite, , . lng. , , SQLite. Activity start .

+1

SQLite . , SharedPreferences.

0

All Articles