0

Hi I have this code which adds a marker when I click on the map but if i rerun the app the marker disappears. Is there any way that I can store the marker somehow and then display it ? I have read about shared preferences but I can't provide the code for it. How can I save the onmapclick action in shared preferences and then display it ?Anyone can help me out ?

 gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){

    @Override
    public void onMapClick(LatLng point) {
        gMap.addMarker(new MarkerOptions().position(point));

    }

});
user3449550
  • 123
  • 5
  • 17

1 Answers1

2

Check this link if you still need any help.

Marker m = null;
SharedPreferences prefs = null;//Place it before onCreate you can access its values any where in this class

// onCreate method started 

prefs = this.getSharedPreferences("LatLng",MODE_PRIVATE); 
//Check whether your preferences contains any values then we get those values
if((prefs.contains("Lat")) && (prefs.contains("Lng"))
{   
String lat = prefs.getString("Lat","");
String lng = prefs.getString("Lng","");    
LatLng l =new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
gMap.addMarker(new MarkerOptions().position(l));

}

Inside your onMapClick

gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){   
@Override
public void onMapClick(LatLng point) {
   marker = gMap.addMarker(new MarkerOptions().position(point));

/* This code will save your location coordinates in SharedPrefrence when you click on the map and later you use it  */
prefs.edit().putString("Lat",String.valueOf(point.latitude)).commit();
prefs.edit().putString("Lng",String.valueOf(point.longitude)).commit();

}

}); 

To remove marker

gMap.setOnMarkerClickListener(new OnMarkerClickListener() {

@Override
public boolean onMarkerClick(Marker arg0) {
//Your marker removed    
marker.remove();
return true;
}
});

How to create custom marker with your own image

// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, lng);

// Changing marker icon
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.your_own_image)));

// adding marker
gMap.addMarker(marker);
Community
  • 1
  • 1
Talha Q
  • 4,350
  • 4
  • 28
  • 40
  • Hey thank you this worked perfectly!!! Any idea of how can i delete one marker now ? – user3449550 Apr 02 '14 at 15:11
  • all right i am now adding some more lines..when you long click then marker will be removed...is it good or you want it at any other point ? – Talha Q Apr 02 '14 at 15:21
  • wait i am writing the code.after testing i will post here :) – Talha Q Apr 02 '14 at 16:02
  • Last question and sorry for this any idea of how I could store lets say a different icon ? cause at the moment i add the marker with the icon that i seted on the onmapclick event but when i got back the default google maps marker is shown. any idea ? – user3449550 Apr 02 '14 at 16:13
  • I have added the code of removing the marker...For using different icon.Check this link http://stackoverflow.com/questions/10077494/android-custom-marker-icon – Talha Q Apr 02 '14 at 16:19
  • No i mean i know how to add a custom icon for the marker but the problem is that in the code you gave me it seems that it doesnt stores the marker's image and its just display the defualt gmaps marker icon. Is there any way to store the icon of the marker in the preferences ? – user3449550 Apr 02 '14 at 16:39
  • I have added the code of custom image marker.Check it out :) – Talha Q Apr 02 '14 at 19:08