问题
I want to get the longitude and latitude co-ordinates of a location and save in a ArrayList, then commit this list to SharedPreference file so when the Map loads all previous locations could be set with a marker using data from the SharedPreference file.
I only know realise that SharedPreference will only store primitive data types and not arrays.
My question does is anybody know of a way to persist this data either via shared pref technique or by any alternate means?
Any input appreciated.
回答1:
1- create a class and put everything you want to store for example arraylist
of your latitude
and longitude
and make that class implement Serializable
interface.
class MyBundle implements Serializable {
ArrayList<Double> mLat;
ArrayList<Double> mLan;
MyBundle( ArrayList<Double> latitude, ArrayList<Double> longitude ){
mLat = latitude;
mLan = longitude;
}
}
2- write it to file:
ObjectOutputStream oos = null;
FileOutputStream fout = null;
try{
FileOutputStream fout = new FileOutputStream("Your file path");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(mb); // mb is an instance of MyBundle
} catch (Exception ex) {
e.printStackTrace();
}finally {
if(oos != null){
oos.close();
}
}
and to get back everything:
ObjectInputStream objectinputstream = null;
try {
streamIn = new FileInputStream("Your file address");
objectinputstream = new ObjectInputStream(streamIn);
MyBundle mb = (MyBundle) objectinputstream.readObject();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(objectinputstream != null){
objectinputstream .close();
}
}
回答2:
You can store them as Strings separated with a period/comma. But in the case of latitude and longitude, you shouldn't use period because they have period (latitude = 27 . 345)
For example
String aLocation = latitude + "," + longitude//store this to sharedpreference
This won't be a problem for longitude and latitude as there is no comma(but has period) that can conflict to your limiter.
And while retrieving the location you can split it and get corresponding longitude and latitude.
String[] locationArray = pref.getString(KEY_LOCATION, "").split(",");
String retrievedLat = locationArray[0];
String retrievedLong = locationArray[1];//you may need to use trim() method
Also you can convert this to JSON and save it. But I would prefer the above method as it's easier and will not give any problem to your scenario.
But in cases where you need to store Strings with special characters like period, you need to make it JSON because it may conflict your limiter
So you can do as following:
public void addToPref(LatLng location){//I'm supposing you are using LatLang class
String aLocation = location.getLatitude() + "," + location.getLongitude();
//and save to shared preference
}
Update:
If you want to enter multiple number of location then you need use JSON(prefered) but the first approach is easier. See below
1. Get string from preference
2. Concatenate it with the new location
Like:
oldLocation,newLocation,newerLocation
P.S. You may have longitude and latitude to each location so the string must be like
{lat1,long1}:{lat2,lat2}:{lat3,long3}
And in this case you need to have multiple limiters and do (split accordingly).
Using split(":") method will give you multiple locations.
And further split it using split(",") that will give you corresponding latitude and longitude.
回答3:
well i used these methods using simple functions
// Saving GeoFence marker with prefs mng
private void saveGeofence() {
Log.d(TAG, "saveGeofence()");
SharedPreferences sharedPref = getPreferences( Context.MODE_PRIVATE );
SharedPreferences.Editor editor = sharedPref.edit();
editor.putLong( KEY_GEOFENCE_LAT, Double.doubleToRawLongBits( geoFenceMarker.getPosition().latitude ));
editor.putLong( KEY_GEOFENCE_LON, Double.doubleToRawLongBits( geoFenceMarker.getPosition().longitude ));
editor.apply();
}
// Recovering last Geofence marker
private void recoverGeofenceMarker() {
Log.d(TAG, "recoverGeofenceMarker");
SharedPreferences sharedPref = getPreferences( Context.MODE_PRIVATE );
if ( sharedPref.contains( KEY_GEOFENCE_LAT ) && sharedPref.contains( KEY_GEOFENCE_LON )) {
double lat = Double.longBitsToDouble( sharedPref.getLong( KEY_GEOFENCE_LAT, -1 ));
double lon = Double.longBitsToDouble( sharedPref.getLong( KEY_GEOFENCE_LON, -1 ));
LatLng latLng = new LatLng( lat, lon );
markerForGeofence(latLng);
drawGeofence();
}
}
来源:https://stackoverflow.com/questions/25836350/how-to-save-latitude-and-longitude-location-in-shared-preference