I have a very basic application that uses the Maps API to display a map, and after a long press it will put a marker down at that location. If you select a different location, it will delete the current marker and make a new one.
The app does what I want it to at the moment, but I can get the data to persist after the app it closed.
This is what I'm currently trying to do:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
BufferedReader reader;
try {
final InputStream file = getAssets().open("userLatLng");
reader = new BufferedReader(new InputStreamReader(file));
String line = reader.readLine();
line = reader.readLine();
System.out.print("Getting Data");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
That is used for reading the data after the application has started
String filename = "userLatLng";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(Integer.parseInt(yayaParking.toString()));
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
that should be how it saves the data. I have yet to get this to work, so any help would be appreciated!
You can save it on the sharedPreferences.
public String getLocation() {
SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
return pref.getString("StoredLocation", "");
}
public void setLocation(String value) {
SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Editor editor = pref.edit();
editor.putString("StoredLocation", value);
editor.commit();
}
you can store and retrieve location using above code.
Make a global variable of SharedPreference.
SharedPreferences preferences;
Save your data as:
private void savePreference(String your_data){
preferences = getSharedPreferences("name_ur_preference", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("DATA", your_data);
editor.apply();
}
Use SharedPreference
API for small data storage.
SharedPreferences sharedPref = context.getSharedPreferences(
"lat_pref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(KEY_LAT, "12.7333"); // your lat and lng
editor.putString(KEY_LNG, "12.7333");
editor.commit();
You can retrieve it like below
SharedPreferences sharedPref = context.getSharedPreferences("lat_pref",Context.MODE_PRIVATE);
String slat = sharedPref.getString(KEY_LAT, "0.0"); //0.0 is default value, you can change it to any value you like. This default value is applied when the key is not present in SharedPrefernce
String slng = sharedPref.getString(KEY_LNG, "0.0");
//Convert String Lat and Lng to double values
double lat = Double.parseDouble(slat);
double lng = Double.parseDouble(slng);
You can declare KEY_LAT & KEY_LNG in some constant file.
public static final String KEY_LAT = "latitude";
public static final String KEY_LNG = "longitude";
If you are in Activity then use this
as context, if in Fragment then use getActivity()
EDIT
editor.putLong(KEY_LAT, Double.doubleToLongBits(location.getLatitude()));
Retrieve it like,
double lat = Double.longBitsToDouble(sharedPref.getLong(KEY_LAT, 0);
Do the same for Longitude
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.