12

I have a problem with onLocationChanged event in Android. Here's the triggering:

case R.id.start: {
    Points.add(overlay.getMyLocation()); // Points' type is ArrayList<GeoPoint>
    mgr.requestLocationUpdates(best, 0, 3, locationListener);
    }
    break;

And here's the onLocationChanged method:

public void onLocationChanged(Location location) {
    i++;
    Points.add(overlay.getMyLocation());
    MapOverlay mapOverlay = new MapOverlay(Points.get(i-1), Points.get(i));
    map.getOverlays().add(mapOverlay); //does the drawing
    mMapController.animateTo(Points.get(i));
}

So, onLocationChanged is called only once and only after I press "start". It's supposed to be called automatically every time the location has changed, right? In my case, it's not.
Please help me.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
user1080051
  • 221
  • 1
  • 2
  • 7

1 Answers1

10

Problem seems to be solved. In onCreate, I added:

Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
best = mgr.getBestProvider(crit, false);
mgr.requestLocationUpdates(best, 0, 1, locationListener);

onLocationChanged now looks like that:

@Override
public void onLocationChanged(Location location) {
    i++;
    nextPoint = overlay.getMyLocation();
    latitude = nextPoint.getLatitudeE6();
    longtitude = nextPoint.getLongitudeE6();
    lastPoint = new GeoPoint((int) latitude, (int) longtitude);
    Points.add(lastPoint);
    MapOverlay mapOverlay = new MapOverlay(Points.get(i - 1), Points.get(i));
    map.getOverlays().add(mapOverlay);
    mMapController.animateTo(Points.get(i));
    nextPoint = null;
    lastPoint = null;
}

Also, very important methods:

@Override
protected void onResume() {
    super.onResume();
    mgr.requestLocationUpdates(best, 10000, 1, locationListener);
}

@Override
protected void onPause() {
    super.onPause();
    mgr.removeUpdates(locationListener);
}

And also some new permissions:

<uses-permission android:name="android.permission.ACCESS_GPS" />
        <uses-permission android:name="android.permission.ACCESS_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.INTERNET" />
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
user1080051
  • 221
  • 1
  • 2
  • 7
  • 11
    `ACCESS_COARSE_LOCATION` is not necessary if you use `ACCESS_FINE_LOCATION`. Just for you to know ;) – lomza Mar 05 '12 at 15:13
  • @lomza, what is the difference? – gumuruh Jul 12 '14 at 06:01
  • 5
    @gumuruh, because `ACCESS_FINE_LOCATION` includes WiFi, cell and GPS location and `ACCESS_COARSE_LOCATION` includes just cell and WiFi. So the first permission basically includes the second one. – lomza Jul 13 '14 at 10:06
  • @lomza, that's good clarification. THanks for it. I just know it from you. :D – gumuruh Jul 14 '14 at 02:56
  • what is `best` , `mgr` and `locationListener` ? – Ravi Vaniya May 21 '18 at 07:18
  • 1
    @RaviVaniya `mgr` is the LocationManager which you can obtain using `locationManager = (LocationManager) requireContext().getSystemService(Context.LOCATION_SERVICE);` - `locationListener` is your implementation of the Android `LocationListener` interface and `best` is a `String` containing the name of the provider which best meets the criteria you passed. – fklappan Dec 03 '19 at 13:02