在android中,如何计算位置从一个位置更改为另一个位置所需的时间

时间:2015-05-25 11:01:23

标签: android google-maps location

我正在开发一个应用程序,用Android设备计算用户的速度。

这是我开发的代码

 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;
            }

但我认为我无法使用 getTime()方法来获取时间。 有没有什么好的解决方案来计算时间

3 个答案:

答案 0 :(得分: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;

从这里回答: https://stackoverflow.com/a/8410867/1796309

答案 1 :(得分:0)

您可以尝试在onCreate()方法中初始化单个变量,从哪里开始节省时间,然后在onLocationChanged()方法中再次获得时间并比较两个时间,例如像这样:

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);

答案 2 :(得分:0)

  1. 获取 startLatitude startLongitude 时获取 startTime
  2. 获取 endlatitude endLongitude
  3. 获取结束时间
  4. 找出它们之间的区别。
  5. 完成!