How to save a marker using SharedPreferences

937 Views Asked by At

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?

2

There are 2 best solutions below

4
On

Do something like this, in your class add

private SharedPreferecnes prefs;

Then when your activity is destroyed

@Override
public void onDestroy(){
     prefs.putDouble("marker_latitude", marker.getPosition().latitude);
     prefs.putDouble("marker_longitude", marker.getPosition().longitude);
}

Then in onCreate

prefs = this.getSharedPreferences("com.smashing_boxes.taskproject",
            Context.MODE_PRIVATE);
double latitude = prefs.getDouble("marker_latitude", -1.0);
double longitude = prefs.getDouble("marker_latitude", -1.0);

if(latitude != -1.0 && longitude != -1.0){
     Marker marker = mapView.addMarker(new MarkerOptions()
          .position(new LatLng(latitude, longitude)));
     myOverlayItems.add(marker);
}

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.

2
On
String fnail LOCATION_KEY = "key":
SharedPreferences preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {

       preferences = getPreferences(MODE_APPEND);

       String location = preferences = getString(LOCATION_KEY ,"default Value");

}


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Editor editor = preferences.edit();
    editor.putString(LOCATION_KEY, "what ever you want");
    editor.commit();

}

this is in generally how to use SharedPreferences just do same thing in your project
i hope this helps