Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am creating an android map application that will get the user location, but when users open their map sometimes the location is not accurate and will slowly pinpoint your location.

I have added a handler that will run every 5 seconds checking the users location, and when the getAccuracy data is either equal or lesser than 100 it will stop. How would I do this?

What I have tried:

I have tried this. But It still calls the handler and the TOASTS won't stop.

Java
<pre>private LocationManager locationManager;

private Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps_page);

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {
              handler.postDelayed(runnable,3000);
        }

        @Override
        public void onProviderDisabled(String provider) {

            handler.removeCallbacks(runnable);
        }
    });

      private Runnable runnable = new Runnable() {
    @Override
    public void run() {

      getCurrentLocation();

        Toast.makeText(mapsPage.this, "GETTING LOCATION", Toast.LENGTH_SHORT).show();

        handler.postDelayed(this, 3000);
    }
};

   private void getCurrentLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    if (location != null) {
        longitude = location.getLongitude();
        latitude = location.getLatitude();

        moveMap();

        Integer loc = Math.round(location.getAccuracy());
        textings.setText(Integer.toString(loc));

        if(loc <= 100)
    {
        handler.removeCallbacks(runnable);

        Toast.makeText(mapsPage.this, "HANDLER STOPPED", Toast.LENGTH_SHORT).show();
    }

    }
}

//Function to move the map
private void moveMap() {

    LatLng latLng = new LatLng(latitude, longitude);

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(17));
    mMap.addMarker(new MarkerOptions().position(latLng).draggable(false));

}
Posted
Updated 3-Jun-17 5:31am

1 solution

As I see it, when the accuracy condition is true, you are removing the handler callback, but the location update is still active. I would think you'd want to remove both.

My suggestion would be to remove the Handler/Runnable code. It is an unnecessary overhead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900