title: How to store GPS Location in SharedPreferences Android
body: <p>I have five fixed GPS locations in my application. When you come near them, it will be stored in the application and unlock a medal of some sort. I have used SharedPreferences for my quiz and medals, but I am a little unsure on how to store GPS in SharedPreferences as well. This is my code so far.</p>
  
  <pre><code>    package com.example.norskattraksjon;
  
      import java.util.ArrayList;
  
      import android.content.Context;
      import android.content.SharedPreferences;
      import android.location.Location;
      import android.location.LocationListener;
      import android.os.Bundle;
  
      import com.google.android.maps.GeoPoint;
  
      public class MyLocationListener implements LocationListener {
  private ArrayList&lt;GeoPoint&gt; gplist;
  
  
  
  public MyLocationListener(ArrayList&lt;GeoPoint&gt; gpList2) {
      gplist = gpList2;   
  }
  
  
  
  public void onLocationChanged(Location location) {
      System.out.println("Den kommer hits");
      for(GeoPoint gp : gplist){
  
          //SÂ henter vi ut latitude og longitude.
          //Det forvirrende her er at klassen "Location" bruker float-koordinater
          //mens "GeoPoint" bruker mikrokoordinater. Dermed mÂ man konvertere mellom de.
          float latitude = (float) (gp.getLatitudeE6() / 1E6);
          float longitude = (float) (gp.getLongitudeE6() / 1E6);
  
          //Vi lager en "location" for hvert av punktene i gplist, utifra latitude og longitude.
          Location test = new Location("tester");
          test.setLatitude(latitude); 
          test.setLongitude(longitude);
  
          //location.distanceTo() er en metode som sjekker avstanden til punkter.
          //Her skal distansen vÊre under 10 000 meter.
          if(location.distanceTo(test) &lt;= 70){
              //Om distansen er under 10 000m, sÂ lager du en toast som gir den faktiske avstanden til punktet.
              System.out.println("Her er du");
  
  
              SharedPreferences prefs = getSharedPreferences("com.example.norskattraksjon",
                      Context.MODE_PRIVATE);
  
                      // Lagre en tekst... først trenger vi en editor                     
                  SharedPreferences.Editor editor = prefs.edit();
                  editor.putBoolean("location", true);
  
                     //Lagrer verdien
                      editor.commit();            
  
          };
  
      }
  
  
  
  
  
  
      };
  
  private SharedPreferences getSharedPreferences(String string,
          int modePrivate) {
      // TODO Auto-generated method stub
      return null;
  }
  
  public void onProviderDisabled(String provider) {
      // TODO Auto-generated method stub
  }
  
  public void onProviderEnabled(String provider) {
      // TODO Auto-generated method stub
  }
  
  public void onStatusChanged(String provider, int status, Bundle extras) {
      // TODO Auto-generated method stub
  }
  
  }
  </code></pre>
  
  <p>And this is my other coherent code;</p>
  
  <pre><code>    package com.example.norskattraksjon;
  
      import java.util.ArrayList;
      import java.util.List;
  
      import android.content.Context;
      import android.content.SharedPreferences;
      import android.graphics.drawable.Drawable;
      import android.location.LocationListener;
      import android.location.LocationManager;
      import android.os.Bundle;
      import android.view.Menu;
  
      import com.google.android.maps.GeoPoint;
      import com.google.android.maps.MapActivity;
      import com.google.android.maps.MapController;
      import com.google.android.maps.MapView;
      import com.google.android.maps.Overlay;
      import com.google.android.maps.OverlayItem;
  
      public class MainActivityMap extends MapActivity {
  
  private MapController mc;
  MapOverlays overlay;
  ArrayList&lt;GeoPoint&gt; gplist;
  List&lt;Overlay&gt; overlayList;
   LocationListener mlocListener; //Denne klassen lytter etter lokasjonen din
    LocationManager mlocManager; //Mens denne kobler deg opp mot telefonens GPS.
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      System.out.println("WEEEEEEE");
      setContentView(R.layout.mapview);
  
  
      MapView mapView = (MapView) findViewById(R.id.mapview);
      mapView.setBuiltInZoomControls(true);
      mapView.setSatellite(true);
  
      mc = mapView.getController();
      gplist = new ArrayList&lt;GeoPoint&gt;();
  
  
      overlayList = mapView.getOverlays();
      Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
      overlay = new MapOverlays(drawable, this);
      double lat = 60.39403;
      double lon = 5.330579;
  
      GeoPoint point = new GeoPoint ((int) (lat * 1E6), (int) (lon*1E6));
      gplist.add(point);
      OverlayItem punkt1 = new OverlayItem(point, "Du har kommet til!", "Domkyrkja i Bergen!");
  
      double lat2 = 60.702251;
      double lon2 = 5.333685;
  
      GeoPoint point1 = new GeoPoint ((int) (lat2 * 1E6), (int) (lon2 *1E6));
      gplist.add(point1);
      OverlayItem punkt2 = new OverlayItem(point1, "Her ble", "Noen som drepte kongen begravd");
  
      double lat3 = 60.37829;
      double lon3 = 5.335665;
  
      GeoPoint point3 = new GeoPoint ((int) (lat3 * 1E6), (int) (lon3 *1E6));
      gplist.add(point3);
      OverlayItem punkt3 = new OverlayItem(point3, "Her ble", "Himmelfarten med tran i maga!");
  
      double lat4 = 60.213789;
      double lon4 = 5.369895;
  
      GeoPoint point4 = new GeoPoint ((int) (lat4 * 1E6), (int) (lon4 *1E6));
      gplist.add(point4);
      OverlayItem punkt4 = new OverlayItem(point4, "Her ble", "Flygelet skapt");
  
      double lat5 = 60.413619;
      double lon5 = 5.37579;
  
      GeoPoint point5 = new GeoPoint ((int) (lat5 * 1E6), (int) (lon5 *1E6));
      gplist.add(point5);
      OverlayItem punkt5 = new OverlayItem(point5, "Her ble", "Bergensbakken bakt");
  
      overlay.addOverlay(punkt1);
      overlay.addOverlay(punkt2);
      overlay.addOverlay(punkt3);
      overlay.addOverlay(punkt4);
      overlay.addOverlay(punkt5);
      overlayList.add(overlay);
  
      mc.animateTo(point);
      mc.setZoom(11);
  
      //Dette er metoden man bruker for Â koble opp til mobilens GPS
        mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  
        //Her lager vi en instans av vÂr egen LocationListener. Dette gj¯r at vi kan bestemme selv hva som skjer
        //nÂr man trykker pÂ overlays.
        mlocListener = new MyLocationListener(gplist);
  
        //Her sier vi at vÂr LocationListener skal sp¯rre om GPS-signaler fra en GPS_PROVIDER.
        //0 stÂr for minimumstiden mellom oppdateringer
        //Den andre 0 stÂr for minimumsdistansen mellom oppdateringer
        //mLocListener er instansen vi lagde av vÂr egen LocationListener.
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
  }
  
  public GeoPoint newGeo(double x, double y){
      int a = (int) (x * 1E6);
      int b = (int) (y * 1E6);
      GeoPoint z = new GeoPoint(a,b);
      return z;
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
  }
  
  @Override
  protected boolean isRouteDisplayed() {
      // TODO Auto-generated method stub
      return false;
  }
  }
  </code></pre>