-1

app runs without errors, i want to calculate distance, when i clicked to button Start it's will be start position and when clicked to Reset - finish position, and it'll calculate distance between Start and Finish position, but it cant calculate distance and speed. How to correct that? here is my code:

public class Tab1 extends Fragment implements LocationListener, View.OnClickListener{

GoogleMap googleMap;
SupportMapFragment mapFragment;
private Button buttonStart;
private Button buttonStop;
private Button buttonReset;
private Chronometer chronometerRun;
private double latStart;
private double lonStart;
private double latStop;
private double lonStop;
private LatLng latLngStart;
private LatLng latLngStop;
private TextView textViewDistance;
Location location;
long time = 0;

@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   View view = inflater.inflate(R.layout.tab_1, container, false);

    textViewDistance = (TextView)view.findViewById(R.id.textViewDistance);
    buttonStart = (Button) view.findViewById(R.id.buttonStart);
    buttonStop = (Button) view.findViewById(R.id.buttonStop);
    buttonReset = (Button) view.findViewById(R.id.buttonReset);
    chronometerRun = (Chronometer) view.findViewById(R.id.chronometerRunning);

    buttonStart.setOnClickListener(this);
    buttonStop.setOnClickListener(this);
    buttonReset.setOnClickListener(this);

    initMap();
    return view;
}

public void initMap(){

    if (googleMap == null) {
        mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

    }

    googleMap = mapFragment.getMap();
    googleMap.setMyLocationEnabled(true);
    // Getting LocationManager object from System Service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    // Creating a criteria object to retrieve provider
    Criteria criteria = new Criteria();
    // Getting the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);
    // Getting Current Location
    location = locationManager.getLastKnownLocation(provider);
    if(location!=null){
        onLocationChanged(location);
    }
    locationManager.requestLocationUpdates(provider, 20000, 0, this);
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);

}

public double CalculationByDistance(LatLng StartP, LatLng EndP) {
    int Radius = 6371;// radius of earth in Km
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
            * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult = Radius * c;
    double km = valueResult / 1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec = Integer.valueOf(newFormat.format(km));
    double meter = valueResult % 1000;
    int meterInDec = Integer.valueOf(newFormat.format(meter));
    Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
            + " Meter   " + meterInDec);

   return Radius * c; //km

}



@Override
public void onClick(View v)
{
    switch(v.getId())
    {
        case R.id.buttonStart:
            chronometerRun.setBase(SystemClock.elapsedRealtime() - time);
            chronometerRun.start();
            latStart = location.getLatitude();
            // Getting longitude
            lonStart = location.getLongitude();
            Toast.makeText(Tab1.super.getActivity() , "latStart: "+latStart+"; lonStart: "+lonStart,Toast.LENGTH_LONG).show();
            // Creating a LatLng object
            latLngStart = new LatLng(latStart, lonStart);
            Log.i("latStart: "+latStart , "lonStart: "+lonStart);
            break;
        case  R.id.buttonStop:
            time = SystemClock.elapsedRealtime() - chronometerRun.getBase();
            chronometerRun.stop();

            break;
        case R.id.buttonReset:
            time = SystemClock.elapsedRealtime() - chronometerRun.getBase();
            Toast.makeText(Tab1.super.getActivity() , "Time: "+time,Toast.LENGTH_SHORT).show();

            latStop = location.getLatitude();
            // Getting longitude of the current location
            lonStop = location.getLongitude();
            Toast.makeText(Tab1.super.getActivity() , "latStop:  "+latStop+"; lonStop: "+lonStop,Toast.LENGTH_LONG).show();
            // Creating a LatLng object for the current location
            latLngStop = new LatLng(latStop, lonStop);
            Log.i("latStop: "+latStop , "lonStop: "+lonStop);
            double distance = CalculationByDistance(latLngStart,latLngStop);
            String sDistance = Double.toString(distance);
            textViewDistance.setText(sDistance);
            if (time > 0 ) {
                double speed = distance / time*1000; // time in ms
                Toast.makeText(Tab1.super.getActivity() ,  "Distance: "+ distance +"; Speed: "+speed,Toast.LENGTH_LONG).show(); }
            time = 0;
            chronometerRun.setBase(SystemClock.elapsedRealtime());
            chronometerRun.stop();
            break;
    }
}

@Override
public void onLocationChanged(Location location) {
    // Getting latitude of the current location
    double latitude = location.getLatitude();
    // Getting longitude of the current location
    double longitude = location.getLongitude();
    // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);
    // Showing the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(14));
}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}
Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
tuan tran
  • 29
  • 4

1 Answers1

0

you dont have to call onLocationChanged method yourself like that, it automatically is called everytime there is a change in the location. You either have to call getLastKNownLocation again in your reset button code and take that location as the last one (like you do in init map) or have a global variable update itself in the onLocationChanged method. So(pseudo-pseudo code):

onLocationChanged(Location location)
{
   globalVarLocation.latitude = location.latitude;
   globalVarLocation.longitude = location.longitude;
}
Nihilanth
  • 401
  • 1
  • 4
  • 8