Question

Hi I have created some code where I save the user's on click event on my shared preferences. So basically on map click a marker is added and when I rerun the app the markers is shown again from the shared preferences. The problem is that for the marker I have setted a different icon, and when I click to add the marker on the map the icon is fine, BUT when I reload the map the icon changes to the default google maps icon. Any advice on this issue ??

SHARED PREFERENCES.


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();

 }

}); 

THE ONCLICK EVENT.

     gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){

        @Override
        public void onMapClick(LatLng point) {
            gMap.addMarker(new MarkerOptions()
            .position(point).
            title("BEACH").
            snippet("added by user.")
             .icon(BitmapDescriptorFactory.fromResource(R.drawable.b)));

    prefss.edit().putString("Lat",String.valueOf(point.latitude)).commit();

    prefss.edit().putString("Lng",String.valueOf(point.longitude)).commit();
        }

    });
Was it helpful?

Solution

You can store the resourceId of the image for each maker in the SharedPreferences. Then you can grab that Id and set the icon by using the icon() method and by passing in BitmapDescriptorFactory.fromResource(resourceId). In other words,

When you want to save the resourceId, use

prefs.edit().putInt(KEY_FOR_MARKER_ICON, iconResourceId);    

Then grab the Id when you want to display it later.

prefs.getInt(KEY_FOR_MARKER_ICON, 0);
markerOptions.icon(BitmapDescriptorFactory.fromResource(resourceId));

I think your code will work if you look at your onCreate() code and just change

marker = gMap.addMarker(new MarkerOptions().position(point));

to

marker = gMap.addMarker(new MarkerOptions().position(point)
                                           .icon(BitmapDescriptorFactory.fromResource(R.drawable.b)));

OTHER TIPS

If you have multiple type of markers(ex:- hospital,school,temple etc..) you should keep them in drawable folder then store the marker type with the latitude and the longitude values in database table... when map load time select all the values from table and check which type of marker is that and place the drawable with the latitude and longitude values

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top