繁体   English   中英

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

[英]In android, how to calculate the time which takes for a location change from one location to another

我正在开发一个应用程序,以使用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()方法来获取时间。 有什么好的解决方法可以计算时间

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

您可以尝试在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);
  1. 获取startLatitudestartLongitude时获取startTime
  2. 获取endlatitudeendLongitude时获取endTime
  3. 找到它们之间的区别。
  4. 做完了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM