Question

1)my android app needs to calculate the accumulate distance when the user press start. when i run, it wait a while then start calculating. which i think may be ok. but when i am not moving, the meters also accumulated by itself. when i tested it while moving it outside, the blue dot is inaccurate and keeps jumping about. this will makes the distance inaccurate. now i retest i think the red marker is me as it displays the "you are here" one time i tested it. it is all over the place. how do i improve the gps accuracy. From what it seems, the meters inaccuracy is due to the gps jumping around and not the calculation. i wanted to make a dot follow me and not many markers appearing where i have been..

2)also, i tried displaying this on emulator but failed(googles maps v2). i did install some apk with newely created emulator but failed too. is it illegal even if it works? also i need to display on the same emulator as my team. as a last resort i can display on my phone.

3)to make google maps v2 work on other computer what do i need to do. my research says i need to sign the API key but so far i found are google maps v1 version.

my java file

public class MainActivity extends FragmentActivity implements LocationListener{

protected LocationManager locationManager;
private GoogleMap googleMap;
Button btnStartMove,btnPause,btnResume,btnStop;
static double n=0;
Long s1,r1;
double dis=0.0;
Thread t1;
EditText userNumberInput;
boolean bool=false;
int count=0;

    double lat1,lon1,lat2,lon2,lat3,lon3,lat4,lon4;
double dist = 0;
    TextView distanceText;
    float[] result;
    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 0;
boolean startDistance = false;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, this);
    if(isGooglePlay())
    {
        setUpMapIfNeeded();
    }
    distanceText=(TextView)findViewById(R.id.Distance);
    btnStartMove=(Button)findViewById(R.id.Start);//start moving
    btnStartMove.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) {
            startDistance = true;
        }
    });
    //prepare distance...........
    Log.d("GPS Enabled", "GPS Enabled");  
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, true);
    Location location=locationManager.getLastKnownLocation(provider);

    if(location!= null)
    {
        //Display current location in Toast
        String message = String.format(
                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(MainActivity.this, message,
                Toast.LENGTH_LONG).show();

        //Display current location in textview  
        //latitude.setText("Current Latitude: " + String.valueOf(location.getLatitude())); 
        //longitude.setText("Current Longitude: " + String.valueOf(location.getLongitude()));
        lat3 = location.getLatitude();
        lon3 = location.getLongitude();
    }
    else if(location == null)
    {
        Toast.makeText(MainActivity.this,
                "Location is null",    
                Toast.LENGTH_LONG).show();
    }


}

private void setUpMapIfNeeded() {

    if(googleMap == null)
    {
        Toast.makeText(MainActivity.this, "Getting map",
                Toast.LENGTH_LONG).show();
        googleMap =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.displayMap)).getMap();

        if(googleMap != null)
        {
            setUpMap();
        }
    }

}

private void setUpMap() 
{
    //Enable MyLocation Layer of Google Map
    googleMap.setMyLocationEnabled(true);

    //Get locationManager object from System Service LOCATION_SERVICE
    //LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    //Create a criteria object to retrieve provider
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    //Get the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);
    if(provider == null)
    {
        onProviderDisabled(provider);
    }
    //set map type
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    //Get current location
    Location myLocation = locationManager.getLastKnownLocation(provider);
    if(myLocation != null)
    {
        onLocationChanged(myLocation);
    }       
    locationManager.requestLocationUpdates(provider, 0, 0, this);
}

private boolean isGooglePlay() 
{
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (status == ConnectionResult.SUCCESS)
    {

        Toast.makeText(MainActivity.this, "Google Play Services is available",
                Toast.LENGTH_LONG).show();
        return(true);
    }
    else
    {
            GooglePlayServicesUtil.getErrorDialog(status, this, 10).show();

    }
    return (false);

 }

@Override
public void onLocationChanged(Location myLocation) {
    //show location on map.................
    //Get latitude of the current location
    double latitude = myLocation.getLatitude();
    //Get longitude of the current location
    double longitude = myLocation.getLongitude();
    //Create a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);
    //Show the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    //Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
    googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));

    //show distance............................
    if(startDistance == true)
    {
        Toast.makeText(MainActivity.this,
                  "Location has changed",    
                  Toast.LENGTH_LONG).show();
            if(myLocation != null)
            {
                //latitude.setText("Current Latitude: " + String.valueOf(loc2.getLatitude())); 
                //longitude.setText("Current Longitude: " + String.valueOf(loc2.getLongitude()));
                float[] results = new float[1]; 
                Location.distanceBetween(lat3, lon3, myLocation.getLatitude(), myLocation.getLongitude(), results);
                System.out.println("Distance is: " + results[0]);               

                dist += results[0];            
                DecimalFormat df = new DecimalFormat("#.##"); // adjust this as appropriate
            if(count==1)
            {
                distanceText.setText(df.format(dist) + "meters");
            }
                lat3=myLocation.getLatitude();
                lon3=myLocation.getLongitude();
                count=1;
          }
    }

}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(MainActivity.this,
            "Provider disabled by the user. GPS turned off",
            Toast.LENGTH_LONG).show();
}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(MainActivity.this,
            "Provider enabled by the user. GPS turned on",
            Toast.LENGTH_LONG).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Toast.makeText(MainActivity.this, "Provider status changed",
            Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, this);
}}

first screenshot, second after a while in seconds, third after 1 hour about.

first shot

second shot

third shot

at the thrid shot, the screen always return back to the place where there are many red markers and not the blue dot. the toast "location has changed" keeps staying on the screen

the main problem is to ensure accurate distance by ensuring accurate gps. i have been troubled a long time by this, i even take away the map from my application since i can't solve it but now i need it.

Was it helpful?

Solution

To remove the jumping locations, ignore all that have location.getSpeed() < speedThreshold.

Choose a speed threshold according to your app needs, but over 5km/h.

(GPS is inacurate if you stand or move slowly)

Additional Info: see my answer at different-gps-location-reading-of-same-physical-location-on-windows-phone-geocoo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top