Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:48:51+00:00 2026-06-15T05:48:51+00:00

I have five fixed GPS locations in my application. When you come near them,

  • 0

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.

    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<GeoPoint> gplist;



public MyLocationListener(ArrayList<GeoPoint> 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) <= 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
}

}

And this is my other coherent 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<GeoPoint> gplist;
List<Overlay> 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<GeoPoint>();


    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;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:48:52+00:00Added an answer on June 15, 2026 at 5:48 am
      SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
        int latE6 = (int) (lat * 1e6);
    int lonE6 = (int) (lon * 1e6);
        p.edit().putInt("Lat", latE6).commit();
        p.edit().putInt("Long", lonE6).commit();
    

    In the above code lat and lon are your location’s latitude and longitude. then you can retrieve them as int variables and get location values by dividing them by 1e6 again.

     int lat= p.getInt("Lat", "");
    int lon = p.getInt("Long", "");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have five panels with some logic in every of them. How could I
I have below five Integer variables and during the program, they are assigned some
I have a Java application that has a fixed thread pool of fifteen, the
Assuming I have a div with a fixed width and some auto wrapping text
I have five buttons on one form that when clicked enables a bool each
I have five divs on my page (#art #fashion, #self, #nightlife, #community). right now,
I have five tables in my database. Members, items, comments, votes and countries. I
I have Five check boxes Search All Template 1 Template 2 Template 3 Template
I have five activities/screens that I would like to be able to swipe between,
I have five tables that are fairly similar in structure but different enough that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.