0

I am developing an app to calculate the user's speed with an android device.

this the code that I have developed

 public void onLocationChanged(Location location) {
        // Assign the new location
        mLastLocation = location;
        location.getLatitude();
        location.getLongitude();

        List<Location> locationsList = new ArrayList<Location>();
        locationsList.add(location);
        avgSpeed = 0;
        speed = 0;
        totalSpeed=0;
        location.getTime();
     for(int i = 0; i < locationsList.size() ; i++) {

                 speed = calculateDistance(locationsList.get(i), locationsList.get(i+1))*1000/location.getTime()*3600;
                 totalSpeed+=speed;
                }
           avgSpeed = totalSpeed/locationsList.size();
}

        private long calculateDistance(Location location, Location location2) {
                // TODO Auto-generated method stub


                double lat1= location.getLatitude();
                double lng1 = location.getLongitude();

                double lat2= location2.getLatitude();
                double lng2 = location2.getLongitude();

                double dLat = Math.toRadians(lat2 - lat1);
                double dLon = Math.toRadians(lng2 - lng1);
                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));
                long distanceInMeters = Math.round(6371000 * c);
                return distanceInMeters;
            }

But I think that i cannot use the getTime() method to get the time. Is there any good solutions to calculate the time

ReB
  • 107
  • 10

3 Answers3

0
Location location1 = new Location("");
location1.setLatitude(lat);
location1.setLongitude(long);

Location location2 = new Location("");
location2.setLatitude(lat);
location2.setLongitude(long);

float distanceInMeters = location1.distanceTo(location2);



//For example spead is 10 meters per minute.

int speedIs10MetersPerMinute = 10;
float estimatedDriveTimeInMinutes = distanceInMeters / speedIs10MetersPerMinute;

Answer from here: https://stackoverflow.com/a/8410867/1796309

Community
  • 1
  • 1
Aleksandr
  • 4,906
  • 4
  • 32
  • 47
  • I think this is for getting the distance . But, I want to to know the time taken for the location change. is there any methods to get the time??? – ReB May 25 '15 at 11:07
  • 1
    First of all, you need to know a distance. If you know distance and speed, you can calculate the time. It is school physics. T = S / V, where S - distance, V - speed – Aleksandr May 25 '15 at 11:10
0

you could try to initialize a single variable in your onCreate() method, where to save the time at the beginning and then in the onLocationChanged() method you could get the time again and compare both times for example like this:

String string1 = "05:00:00 PM";
Date time1 = new SimpleDateFormat("HH:mm:ss aa").parse(string1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);

String string2 = "09:00:00 AM";
Date time2 = new SimpleDateFormat("HH:mm:ss aa").parse(string2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);

Date x = calendar1.getTime();
Date xy = calendar2.getTime();
long diff = x.getTime() - xy.getTime();
diffMinutes = diff / (60 * 1000);
float diffHours = diffMinutes / 60;
System.out.println("diff hours" + diffHours);
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
0
  1. Get startTime when getting startLatitude and startLongitude.
  2. Get endTime when getting endlatitude and endLongitude.
  3. Find the difference between them.
  4. Done!
activesince93
  • 1,716
  • 17
  • 37