【发布时间】: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