0

First of all I'm a complete noob to android. Going step by step on my first app and facing some problems.

  1. I can get my location in terms of Lat and Lon and now i have to save it to file and able to read the file to compare location in future. Could anybody please help me out on this can be done.

Following is my INCORRECT CODE

public void saveCurrentLocation(Location location){
SharedPreferences prefs = this.getSharedPreferences("com.example.mylocation", Context.MODE_PRIVATE);

String currentLat = "com.example.mylocation.location";

String now = prefs.getString(currentLat, location.getLatitude());
}

Error shown is that location.getLatitude is a double and cannot be saved to string (quite obvious but not sure how to change it)

Thanks

  • You can convert doubles into a string (have a look at the [Double class](http://developer.android.com/reference/java/lang/Double.html)), or use floats (but I assume they are not precise enough for location data). – dst Aug 11 '13 at 16:49
  • I suggest you to refer following link http://stackoverflow.com/a/17222144/1554935 – Ritesh Gune Aug 11 '13 at 16:58

2 Answers2

0
location.getLatitude() + "";

In Java, the + operator is overloaded to concatenate Strings. If you add "" to anything, it will be automatically cast to String.

Radu
  • 384
  • 4
  • 15
0

If you want to store the result of location.getLatitude() in your sharedpreferences try converting the double to a String:

String.valueOf(location.getLatitude())
Naroh
  • 625
  • 4
  • 9