有没有办法计算Android Maps V2中当前骑行的速度

时间:2013-09-25 09:23:04

标签: android google-maps-android-api-2

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

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

2 个答案:

答案 0 :(得分:2)

  

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

所以要计算当前速度,计算你得到的两个最后位置之间的距离(通过Location.distanceTo())并除以该距离所需的时间,这是时间戳的差异。地点。

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

即可

答案 1 :(得分: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();
        }

    }