Petar
Petar

Reputation: 2283

Android gps : how to check if a certain point has been reached?

I am working on an Android app and I need to make sure that the device has reached a certain point described with lat and lon. The only thing that I can think of is to have something like that :

    Location initLoc = theManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double lat = initLoc.getLatitude();
    double lon = initLoc.getLongitude();
    MyPoint firstPoint = getPoints().get(0);

    double dist = CalcHelper.getDistance1(lat, lat, firstPoint.getLat(), firstPoint.getLon());

    while(dist > 30){

        initLoc = theManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        lat = initLoc.getLatitude();
        lon = initLoc.getLongitude();
        dist = CalcHelper.getDistance1(lat, lon, firstPoint.getLat(), firstPoint.getLon());

    }

But that causes the program to crash. I would be very appreciative if you could lead me to the right direction here.

Let me take the opportunity to ask a further question. As I said I am new to Android and GPS and having in mind there is little documentation and information on how to properly develop applications that work with GPS I am working in blind basically. So my question is:

This is how the onLocationChanged method looks like:

            public void onLocationChanged(Location location) {

            double lat = location.getLatitude();
            double lon = location.getLongitude();
            MyPoint firstPoint = MainScreen.dataset.getPoints().get(0);

            double dist = CalcHelper.getDistance1(lat, firstPoint.getLat(),lon, firstPoint.getLon());

            if(dist < 10){

                Context context = getApplicationContext();
                CharSequence text = "Start reached. Starting moving on track";
                int duration = 6000;
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();   

            }else{

                Context context = getApplicationContext();
                CharSequence text = "Distance until start: "+dist;
                int duration = 6000;
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();

            }

    }

What I am trying to do is to make the program determine when I have reached the start of a track I have supplied as a set of points. So, when I start the program I receive a reasonable estimate on the distance. The problem is, when I start moving, the distance does not seem to be updated, it gets updated but after moving 50meters, it says i have only moved 5 say. On the other hand, when I start the application and I am less than 10m away from the starting point, it detects it properly. So basically, while I am moving with the device, the onLocationChanged method does not seem to give me the correct location that I am now on. Can you tell me what I might be doing wrong ?

Upvotes: 2

Views: 1786

Answers (2)

n3utrino
n3utrino

Reputation: 2381

Look at this it might be exactly what you're needing

Proximity Alert

Upvotes: 1

Ilya Saunkin
Ilya Saunkin

Reputation: 19810

I think what you want to do is to make your activity implement LocationListener, i.e. subscribe to the GPS fix. E.g.

public class MyActivity extends Activity implements LocationListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // request current location updates
        LocationManager locMan = (LocationManager) getSystemService(LOCATION_SERVICE);
        // set here the criteria for location provider accuracy
        Criteria locationProviderCriteria = new Criteria();
        locationProviderCriteria.setAccuracy(Criteria.ACCURACY_FINE);
        String locationProvider = locMan.getBestProvider(locationProviderCriteria, true);
        locMan.requestLocationUpdates(locationProvider, MIN_TIME_BETWEEN_UPDATES_IN_MILLIS, MIN_DISTANCE_BETWEEN_UPDATES_IN_METERS, this);
    }

    /*
     * This method will be called on each location update
     */
    @Override
    public void onLocationChanged(Location loc) {
        //put here your logic to see whether the user reached the destination
    }
}

Upvotes: 2

Related Questions