【问题标题】:In android, how to calculate the time which takes for a location change from one location to another在android中,如何计算位置从一个位置更改到另一个位置所需的时间
【发布时间】:2015-08-06 20:17:06
【问题描述】:

我正在开发一个应用来计算用户在安卓设备上的速度。

这是我开发的代码

 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() 方法来获取时间。 有什么好的方法可以计算时间

【问题讨论】:

    标签: android google-maps location


    【解决方案1】:
    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

    【讨论】:

    • 我认为这是为了获取距离。但是,我想知道位置更改所花费的时间。有什么方法可以获取时间???
    • 首先,你需要知道一个距离。如果你知道距离和速度,你就可以计算时间。这是学校物理。 T = S / V,其中 S - 距离,V - 速度
    【解决方案2】:

    您可以尝试在您的 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);
    

    【讨论】:

      【解决方案3】:
      1. 获取startLatitudestartLongitude时获取startTime
      2. 获取endlatitudeendLongitude时获取endTime
      3. 找出它们之间的区别。
      4. 完成!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-07
        • 2014-11-25
        • 1970-01-01
        • 1970-01-01
        • 2019-11-21
        • 1970-01-01
        • 2020-08-13
        • 1970-01-01
        相关资源
        最近更新 更多