tags:

views:

69

answers:

2

Hi,

On onLocationChanged event I want to save my GPS route (Latitude , Longitude). Later I want to load this data and draw the route.

Who is the best way to do this, by using some array type (and save or load by using database) or XML files or something else?

Thanks.

A: 

Use SharedPreferences and Editor.

Check out this open source code, it will probably help you a lot: OsmandSettings.java

I'll explain the important parts of the code:

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

// These settings are stored in SharedPreferences, replace com.osmand.settings 
// with your own package name, or whatever String you want.
public static final String SHARED_PREFERENCES_NAME = "com.osmand.settings";
public static final String LATITUDE = "latitude";
public static final String LONGITUDE = "longitude";

To write to SharedPreferences:

public void onLocationChanged(Location location){
    SharedPreferences prefs = Context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
    Editor editor = prefs.edit();
    //Save it as a float since SharedPreferences can't deal with doubles
    edit.putFloat(LATITUDE, (float) Location.getLatitude());
    edit.putFloat(LONGITUDE, (float) Location.getLongitude());
    edit.commit();
}

To read from SharedPreferences:

public void onLocationChanged(Location location){
    SharedPreferences prefs = Context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_WORLD_READABLE);
    double lat = (double)prefs.getFloat(LATITUDE, 0);
    double lon = (double)prefs.getFloat(LONGITUDE, 0);
}
magicman
This will only store the last location. Just store them in an ArrayList or something
Falmarri
A: 

Hi, I also had same problem while i was doing app on Map. In fact good question i would say.

you can do like this.

1> On Click event on Map u have to show latitude and longitude of that point. here i m attaching code how u can find lat and long.

public boolean onTouchEvent(MotionEvent event, MapView mapView) {
//---when user lifts his finger--- if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Toast.makeText(getBaseContext(), p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show(); }
return false; }

2> Now u can store this lat and long in KEY,value pair. here u have to take value1 as lat and value 2 as long. and pass the object of
value(value1,value2) to key,value pair.

3> Now u have the list of all the lat and long through which you have passed. then draw route from 1st index to last index.

This is what i think the best way to implement this app.

Rakesh Gondaliya