1

Im facing some problem with my application, specifically with route plot/draw on my google maps. Ive made test route around my house and found out, that GPS providers are not as accurate as similar applications like runtastic or endomondo.

Sometimes Location listener makes incomprehensible changes on my map and the polyline then draws any lines on the map near my location even with perfect GPS signal.
Some other time, it just doesnot work. It does not listen to location change.

Can anybody explain me (like Im five) how does other fitness application get their current position and the plot route onto the google map? Thanks!

//Map Fragment

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.fragment1)).getMap();
    map.setMyLocationEnabled(true);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);

//Permission gain

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                MY_PERMISSION_ACCESS_COURSE_LOCATION);
        return;
    }
    isLocationEnabled(getApplicationContext());

    Location myLocation = locationManager.getLastKnownLocation(provider);
    if (myLocation != null) {
        latitude = myLocation.getLatitude();
        longitude = myLocation.getLongitude();
        float zoom = (float) 17.0;
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), zoom));
        Log.e("TAG", "GPS is on");
        Toast.makeText(MainActivity.this, "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();
    } else {
        locationManager.requestLocationUpdates(provider, 5000, 0, this);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, this);

    }

 public void onLocationChanged(Location mylocation) {

    if (lastLocationloc == null) {
        lastLocationloc = mylocation;
    }
    LatLng lastLatLng = locationToLatLng(lastLocationloc);
    LatLng thisLatLng = locationToLatLng(mylocation);
    map.addPolyline(new PolylineOptions().add(lastLatLng).add(thisLatLng).width(6).color(Color.RED));
    lastLocationloc = mylocation;
    Toast.makeText(MainActivity.this, "!!!!Location CHANGE!!!!", Toast.LENGTH_SHORT).show();

}

public static LatLng locationToLatLng(Location loc) {
    if (loc != null)
        return new LatLng(loc.getLatitude(), loc.getLongitude());
    return null;
}


For example, I want to plot it like this:


But my application works its own way..

1 Answers1

0

First of all, you need to take into account the accuracy of the received location. You can get the accuracy using the Location.getAccuracy() method (documentation). The accuracy is measured in meters, so the lower the better:

if (location.getAccuracy() < MINIMUM_ACCURACY) {
    // Add the new location to your polyline
}

You can set your MINIMUM_ACCURACY to be 10 meter for example.

On the other hand, you may want to add a new location to your polyline only if your new location is farther than a given distance to your last added location. As an example:

private static final float MINIMUM_ACCURACY = 10;
private static final float MINIMUM_DISTANCE_BETWEEN_POINTS = 20;
private Location lastLocationloc;

// ...

public void onLocationChanged(Location mylocation) {
    if (mylocation.getAccuracy() < MINIMUM_ACCURACY) {
        if (lastLocationloc == null || lastLocationloc.distanceTo(mylocation) > MINIMUM_DISTANCE_BETWEEN_POINTS) {
            // Add the new location to your polyline

            lastLocationloc = mylocation;
        }
    }
}
antonio
  • 18,044
  • 4
  • 45
  • 61
  • It works like a charm, altough it has some mistakes. As you can [see](http://i.imgur.com/kXTbXhT.png), my first journey have stopped ploting after some time. I ve walked forward and then started to walk the other way - it worked smooth.. but then the locationchange has just dissapear until Ive came to the [bridge or so (the yellow road)](http://i.imgur.com/9uXtwao.png). Does that mean it has lack of GPS signal just because ive been between two buildings? – Pedro Ewors May 14 '16 at 13:24
  • If you suspect that you may have problems with the location provider, you may want to take a look at the Fused Location Provider http://stackoverflow.com/questions/6775257/android-location-providers-gps-or-network-provider – antonio May 14 '16 at 13:58
  • Could you please explain, how to implement fused location provider to my code? Is it replacement for regular location listener or? – Pedro Ewors May 14 '16 at 21:32