Pretty lame title, but that's what I am trying to figure out. I want simply to be able to create markers on long click on the map, and then save those markers in db, preferences, array list, I don't know, just in some persistent storage, where they won't be wiped on the next start of application. I want to keep it simple, if it's possible with just shared preference, then please tell me how. Here's the code
public class MainActivity extends Activity implements ParsingCallback {
private GoogleMap mMap;
Marker marker = null;
String s;
GoogleMapOptions options;
LocationManager lm;
LatLng userLocation;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
options = new GoogleMapOptions();
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listOfPoints = new ArrayList<SavedMarkers>();
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setRotateGesturesEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}
/** On Map Long Click */
mMap.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng arg0) {
mMap.addMarker(new MarkerOptions().position(arg0).title(arg0.toString()));
Toast.makeText(getApplicationContext(), "Marker Created", Toast.LENGTH_SHORT).show();
}
});
}
}
Use Gson to serialize and deserialize your array list of SavedMarkers
.
Type listOfMarkers = new TypeToken<List<SavedMarkers>>(){}.getType();
String stringToSaveInSharedPreferences = gson.toJson(list, listOfMarkers);
List<SavedMarker> markers = gson.fromJson(stringRetrievedFromSharedPreferences,listOfMarkers)
;