I'm using Mapsforge Library to develop my app. In the map, you can create your own mark by long press on the map.
Now i would like to save this marker using sharedPreferences, so if the user close the app and repp, the marker still there. Unfortunately, i have not found such information around the web on how to do so. I have found some information on how to do it using Google maps, but google map have different functions. Can anyone help me how to do it?
this is how i add the mark on the map. the code is in the OnCreate part:
myListOverlay = new ListOverlay();
myOverlayItems = myListOverlay.getOverlayItems();
mapView.getOverlays().add(myListOverlay);
mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public void onLongPress(MotionEvent e) {
float x = e.getX();
float y = e.getY();
if (e.getAction() == MotionEvent.ACTION_DOWN) {
GeoPoint gPt = mapView.getProjection().fromPixels((int) x, (int)y);
myMarker = createMarker(gPt, R.drawable.ic_location);
myOverlayItems.add(myMarker);
byte viewMarker = mapView.getMapViewPosition().getZoomLevel();
mapView.getMapViewPosition().setZoomLevel(viewMarker);
}
}
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
myOverlayItems.remove(myMarker);
byte viewMarker = mapView.getMapViewPosition().getZoomLevel();
mapView.getMapViewPosition().setZoomLevel(viewMarker);
return true;
};
}
);
mapView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v0, MotionEvent e) {
// Only fires if Mapview touched
return mGestureDetector.onTouchEvent(e);
}
});
Thanks to zcg7009 i was able to let the shared preferences work, but still a problem. This is the code:
in onCreate
if((prefs.contains("Lat")) && (prefs.contains("Lon"))){
String lat = prefs.getString("Lat",""); //with "" it throw error " Invalid "" ", if i put "0" the marker appear somewhere in the map
String lng = prefs.getString("Lng", "");
GeoPoint l =new GeoPoint(Double.parseDouble(lat),Double.parseDouble(lng));
myMarker = createMarker(l, R.drawable.ic_location);
myOverlayItems.add(myMarker);
byte viewMarker = this.mapView.getMapViewPosition().getZoomLevel();
this.mapView.getMapViewPosition().setZoomLevel(viewMarker);
this.mapView.getMapViewPosition().setCenter(l);
}
In the LongPress method
float x = e.getX();
float y = e.getY();
GeoPoint gPt = mapView.getProjection().fromPixels((int)x, (int)y);
myMarker = createMarker(gPt, R.drawable.ic_location);
myOverlayItems.add(myMarker);
byte viewMarker = mapView.getMapViewPosition().getZoomLevel();
mapView.getMapViewPosition().setZoomLevel(viewMarker);
prefs.edit().putString("Lat",String.valueOf(gPt.latitude)).commit();
prefs.edit().putString("Lon",String.valueOf(gPt.latitude)).commit();
the problem is in the second and third line of the code inside onCreate. If i use "" it throw the error invalid "", if i use "0" , the marker appear somewhere on the map. How can solve this?
Do something like this, in your class add
Then when your activity is destroyed
Then in onCreate
This says that if you have stored a latitude and longitude in your preferences, you want to draw the marker on your map at the given coordinates and add the marker to your overlay items list.