Question

I am using Android Google maps v2 API and have it set up to add markers on long click. I need a way to save these markers and reload them when the app resumes again. What will be the best way to do this? Please help

Currently I add markers as follows:

map.addMarker(new MarkerOptions().position(latlonpoint)
            .icon(bitmapDescriptor).title(latlonpoint.toString()));
Était-ce utile?

La solution

I got it! I can easily do this via saving the array list of points to a file and then reading them back from file

I do the following onPause:

try {
    // Modes: MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITABLE
    FileOutputStream output = openFileOutput("latlngpoints.txt",
    Context.MODE_PRIVATE);
    DataOutputStream dout = new DataOutputStream(output);
    dout.writeInt(listOfPoints.size()); // Save line count
    for (LatLng point : listOfPoints) {
        dout.writeUTF(point.latitude + "," + point.longitude);
        Log.v("write", point.latitude + "," + point.longitude);
    }
    dout.flush(); // Flush stream ...
    dout.close(); // ... and close.
} catch (IOException exc) {
    exc.printStackTrace();
}

And onResume: I do the opposite

try {
    FileInputStream input = openFileInput("latlngpoints.txt");
    DataInputStream din = new DataInputStream(input);
    int sz = din.readInt(); // Read line count
    for (int i = 0; i < sz; i++) {
        String str = din.readUTF();
        Log.v("read", str);
        String[] stringArray = str.split(",");
        double latitude = Double.parseDouble(stringArray[0]);
        double longitude = Double.parseDouble(stringArray[1]);
        listOfPoints.add(new LatLng(latitude, longitude));
    }
    din.close();
    loadMarkers(listOfPoints);
} catch (IOException exc) {
    exc.printStackTrace();
}

Autres conseils

You can implement the onLongClickListener for the marker as below :

map.addMarker(new MarkerOptions()
    .position(latlonpoint)
    .icon(bitmapDescriptor)
    .title(latlonpoint.toString()));
map.setOnMapLongClickListener(new OnMapLongClickListener() {
    @Override
    public void onMapLongClick(LatLng p_point) {
        // TODO ...
    }
});

First save latitude and longitude on long click in the database

Note:-ignore place no need for that

 HistoryModel historyModel = new HistoryModel(place,sLatitude, sLongitude);
 DatabaseMethods.openDB(MapsActivity.this);
 DatabaseMethods.addHistory(historyModel);
 DatabaseMethods.closeDB();

In onMapReady callback

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    DatabaseFunctions.openDB(MapsActivity.this);
    ArrayList<HistoryModel> historyModelArr = DatabaseFunctions.getHistory();
    DatabaseFunctions.closeDB();

    for (int i = 0; i < historyModelArr.size(); i++) {
        double latitude = Double.parseDouble(historyModelArr.get(i).getLatitude());
        double longitude = Double.parseDouble(historyModelArr.get(i).getLongitude());
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude)));
    }
}

Used Methods :-

   public static ArrayList<HistoryModel> getHistory() {
    Cursor cursor = null;
    ArrayList<HistoryModel> historyModelArr = null;
    try {
        cursor = db.rawQuery("SELECT * FROM " + DBHelper.TABLE_HISTORY,
                null);

        if (cursor != null) {
            historyModelArr = new ArrayList<HistoryModel>();
            while (cursor.moveToNext()) {
                HistoryModel historyModel = new HistoryModel(cursor.getString(1),cursor.getString(2), cursor.getString(3));
                historyModelArr.add(historyModel);
            }
            return historyModelArr;
        } else {
            return historyModelArr;
        }

    } catch (Exception e) {
        Log.e(tag, "getHistory Error : " + e.toString());
        return historyModelArr;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}



public static void addHistory(HistoryModel historyModel) {

    ContentValues values;
    try {
        values = new ContentValues();
        values.put(DBHelper.HISTORY_PLACE, historyModel.getPlace());
        values.put(DBHelper.HISTORY_LATITUDE, historyModel.getLatitude());
        values.put(DBHelper.HISTORY_LONGITUDE, historyModel.getLongitude());
        db.insert(DBHelper.TABLE_HISTORY, null, values);
    } catch (Exception e) {
       
    }

}

Model Class

public class HistoryModel {

private String place, latitude, longitude;

public HistoryModel(String place, String latitude, String longitude) {
    this.place = place;
    this.latitude = latitude;
    this.longitude = longitude;
}


public String getPlace() {
    return place;
}

public void setPlace(String place) {
    this.place = place;
}

public String getLatitude() {
    return latitude;
}

public void setLatitude(String latitude) {
    this.latitude = latitude;
}

public String getLongitude() {
    return longitude;
}

public void setLongitude(String longitude) {
    this.longitude = longitude;
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top