【问题标题】:Calculate average speed from array of Coordinates从坐标数组计算平均速度
【发布时间】:2018-03-19 16:22:37
【问题描述】:
     //Global var
     ArrayList<LatLng> points = new ArrayList<>(); 

//--------------------------------------------- ------------------------------------------

public void onLocationChanged(final Location location) {
    //Adds a marker on the current position found in LatLng
    myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());

    //Sets the marker to the position of LatLng and zooms the camera in on the location
    LocationMarker.setPosition(myCoordinates);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(myCoordinates));
    float zoomLevel = 12.0f;
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myCoordinates, zoomLevel));

    //Adds marker on each location update
    points.add(myCoordinates);

    mMap.addMarker(new MarkerOptions().position(points.get(0))
            .title("Starting Location")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));

    //Draws a polyline between the location updates stored in points.
    mMap.addPolyline(new PolylineOptions()
            .addAll(points)
            .width(5)
            .color(Color.RED));

    //gets speed from Location and converts it to Km/h
    speedText.setText(String.format("%.0f - Km/h", location.getSpeed() * 3.6));

}

这是我的 onLocationChanged 函数。这会在每次更新时将坐标存储到我的名为 points 的数组中。它还会在每次更新时绘制一条折线,以公里/小时为单位计算我的速度,并在我的数组位置 [0] 处添加一个标记,这是我的起始位置。这一切都很好,非常适合我需要做的事情。

是否可以由此计算平均速度?

【问题讨论】:

  • 你需要计算每两个连续坐标之间的速度,然后计算平均速度
  • 还看一下 SphericalUtil.computeLength - 它需要一个 LatLng 的列表并返回总路径长度 - 所以这个长度和第一点的时间和当前时间你可以计算平均速度。顺便说一句,您的“addPolyLine”会不断添加一条越来越长的折线,而不会删除您之前无法想象的折线。

标签: android google-maps android-gps


【解决方案1】:

只需要再创建一个全局列表,插入getSpeed()的值;

speedArray.add(Double.parseDouble(location.getSpeed()));

并创建一个在 onLocationChanged 结束时调用的函数,并计算每次的平均速度;

【讨论】:

  • getSpeed 仅当您之前使用 setSpeed 设置它们时才会返回有效值。否则它将返回 0(零)。
  • 谢谢@dan 我猜他以前有速度值。
  • 不客气。请注意,您使用的 location 对象由回调接收。他需要使用之前的 GPS 位置来计算速度。
  • 谢谢安东尼奥!不太确定从哪里开始。
【解决方案2】:

是的,是的。但是你需要记录更多的数据,比如:

  • 以前的位置坐标。
  • 位置样本的当前时间。

使用上述信息,您可以使用以下公式: speed = distance / time

distance 将是 currentGPSPoint - previousGPSPointtime = currentTime - previousTime

这将为您提供两个位置之间的平均速度。如果您需要总体平均值,您可以保存所有计算出的速度并计算平均值。或者只存储初始位置并始终计算从初始位置到当前位置的速度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多