0

我可以在驾驶或步行时动态地从起始位置绘制一条折线,我只需要计算当前骑行在特定时间间隔内的速度。

请建议我如何使用 GPS/网络服务计算速度。

4

2 回答 2

2

速度:行驶距离除以行驶时间。

因此,要计算当前速度,请计算您获得的最后两个位置之间的距离(通过 Location.distanceTo())并除以该距离所需的时间,即位置时间戳的差异。

或者,只需使用Location.getSpeed()

于 2013-09-25T09:28:58.753 回答
1

检查下面的代码,由我完成,它运行良好。在这么多与您的东西相关的代码中,因为我所做的与您的要求完全相同。所以你可以使用下面给出的所有代码。如果你觉得它不必要,那就不要使用它。

@Override
    public void onLocationChanged(Location location) {

        try {
            if (location != null) {

                if (current_lat != null && current_lat > 0) {
                    latitude = current_lat;
                }
                if (current_long != null && current_long > 0) {
                    longitude = current_long;
                }
                current_lat = location.getLatitude();
                current_long = location.getLongitude();
                distanceBetweenTwoPoint = getDistance(latitude, longitude, current_lat, current_long);
                if ((current_lat > 0 && current_long > 0) && distanceBetweenTwoPoint > IjoomerApplicationConfiguration.track_DistanceBetweenPoints_IN_METERS) {
                    if (location.hasSpeed()) {
                        speedInKm = location.getSpeed() * 3.6;
                    } else {
                        speedInKm = 0.0;
                    }
                    row = new HashMap<String, String>();
                    row.put(TRACKID, IN_TRACKID + "");
                    row.put(LATITUDE, current_lat.toString());
                    row.put(LONGITUDE, current_long.toString());
                    row.put(SPEED, speedInKm + "");
                    row.put(TIMESTAMP, System.currentTimeMillis() + "");
                    row.put(STATUS, status);

                    distance = distance + (distanceBetweenTwoPoint / 1000);
                    row.put(DISTANCE, "" + distance);
                    dataProvider.InsertRow("TrackDetail", row);
                    row.put(LASTKNOWNLATITUDE, latitude.toString());
                    row.put(LASTKNOWNLONGITUDE, longitude.toString());
                    int seconds = (int) ((System.currentTimeMillis() - trackStartTime) / 1000) % 60;
                    int minutes = (int) (((System.currentTimeMillis() - trackStartTime) / (1000 * 60)) % 60);
                    int hours = (int) (((System.currentTimeMillis() - trackStartTime) / (1000 * 60 * 60)) % 24);

                    row.put(DURATION, String.valueOf(hours) + " : " + String.valueOf(minutes) + " : " + String.valueOf(seconds));
                    setNotification(speedInKm, String.valueOf(hours) + " : " + String.valueOf(minutes) + " : " + String.valueOf(seconds));
                    if (status.equalsIgnoreCase("1")) {

                        builder.append("|" + current_lat + "," + current_long);
                        trackDuration = String.valueOf(hours) + " : " + String.valueOf(minutes) + " : " + String.valueOf(seconds);
                        if (speedInKm > maxSpeed) {
                            maxSpeed = speedInKm;
                        }
                        totalSpeed = totalSpeed + speedInKm;
                        ++totalTrackPoint;
                        sendBrodcastToActivity();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
于 2013-09-25T09:26:03.333 回答