根据我的曲目详细信息跟踪距离和时间

时间:2011-09-01 06:02:20

标签: android google-maps google-direction

我正在使用android中的gps跟踪来跟踪用户位置并提供记录轨道的功能。我现在能够绘制路径我想要计算轨道距离和时间,就像假设用户开始跟踪记录和移动到另一个位置现在我想计算谷歌地图中从开始到结束位置(用户位置更新)的总距离和时间旅行。我有计算2个位置的距离但不适合我的路线的功能,因为路线是折线,它是灵活的纬度/直线位置。是他们的任何api或谷歌为此提供的任何功能或服务。任何帮助或建议都表示赞赏。

3 个答案:

答案 0 :(得分:6)

我会创建一个名为waypoint的类

class WayPoint
{
   DateTime depart; //some date time container
   DateTime arrive; //some date time container
   Coordinate position; //some gps coordinate
}

然后创建这些类的列表,允许在任何位置插入元素,这对您的路线更改很有帮助:

List<WayPoint> journey = new ArrayList<WayPoint>();

//just add your waypoints

journey.add(startWayPoint);
journey.add(wayPoint_1);
journey.add(wayPoint_2);
//...
journey.add(wayPoint_n_minus_2);
journey.add(wayPoint_n_minus_1);
journey.add(endWayPoint);

然后转换为数组并计算总数:

WayPoint[] wayPoints = journey.toArray();

double total_distance = 0.0f; //distance in metres
double total_travel_time = 0.0f; // time in hours

//start at index 1 because there are n-1 segments
if(wayPoints.length>1)
foreach(int i=1; i<wayPoints.length;i++)
{
  total_distance += calcDistanceBetween(
      wayPoints[i-1].position,
      wayPoints[i].position);

  total_time += calcTimeInHoursBetween(
      wayPoints[i-1].depart,
      wayPoints[i].arrive);
}

log.d("Total Distance",String.valueOf(total_distance));
log.d("Total Travel Time",String.valueOf(total_travel_time));

答案 1 :(得分:2)

我按照自己的方式实现,我创建了内部类,扩展了Overlay类以在地图上绘制路径/路径

private class TrackOverlay extends Overlay {
    private List<GeoPoint> polyline;

    private Paint mPaint;
    private Point p1;
    public TrackOverlay() {
        polyline = new ArrayList<GeoPoint>();

        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(5);
        mPaint.setARGB(150, 62, 184, 240);

        p1 = new Point();
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, shadow);
        if (drawTrack && polyline.size() > 0) {
            mPaint.setARGB(120, 212, 51, 51);
            drawTrackPath(canvas);
        }
        if (showTrack && polyline.size() > 0) {
            mPaint.setARGB(150, 62, 184, 240);
            drawTrackPath(canvas);
        }
    }

    private void drawTrackPath(Canvas canvas) {
        int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
        for (GeoPoint gp : polyline) {
            mapView.getProjection().toPixels(gp, p1);
            x2 = p1.x;
            y2 = p1.y;

            if (x1 != 0 && y1 != 0) {
                canvas.drawLine(x1, y1, x2, y2, mPaint);
            }
            x1 = x2;
            y1 = y2;
        }
    }

    void addTrackPoint(GeoPoint geoPoint) {
        polyline.add(geoPoint);
    }

    List<GeoPoint> getPolylineTrack() {
        return polyline;
    }
}

创建此类的新对象并以这种方式添加到地图叠加层中

trackOverlay = new TrackOverlay();
mapView.getOverlays().add(trackOverlay);

现在我已经绘制路径当用户点击按钮记录轨道并找到我创建更新方法的总距离和时间,当gps获得新位置时,将从locationChange()方法调用该位置传递给map活动并存储到TrackOverlay类的折线对象中。

public static void updateMap() {
    if (ServiceLocation.curLocation != null) {
        curTime = ServiceLocation.curLocation.getTime();
        curLat = ServiceLocation.curLocation.getLatitude();
        curLng = ServiceLocation.curLocation.getLongitude();

        if (mapView != null) {
            point = new GeoPoint((int) (curLat * 1e6), (int) (curLng * 1e6));

            mc.animateTo(point);
            if (drawTrack && trackOverlay != null) {
                trackOverlay.addTrackPoint(point);
                if(prevTime>0)
                    totalSec += (curTime-prevTime);

                double x1 = 0, x2 = 0, y1 = 0, y2 = 0, temp_dist=0,temp_speed=0;
                if(trackOverlay.polyline.size()>1){
                    x1 = trackOverlay.polyline.get(trackOverlay.polyline.size()-2).getLatitudeE6()/1e6;
                    y1 = trackOverlay.polyline.get(trackOverlay.polyline.size()-2).getLongitudeE6()/1e6;

                    x2 = trackOverlay.polyline.get(trackOverlay.polyline.size()-1).getLatitudeE6()/1e6;
                    y2 = trackOverlay.polyline.get(trackOverlay.polyline.size()-1).getLongitudeE6()/1e6;

                    dist += (Geo_Class.distFrom(x1, y1, x2, y2) / METER_KILOMETER);

                    double totalMeter = dist * METER_KILOMETER;
                    double total_sec = (totalSec/1000) * KILOMETER_HOUR;

                    speed = totalMeter/total_sec;

                    txt_msg.setText("Distance " + round(dist,5,BigDecimal.ROUND_HALF_UP) + " km");
                    speed_msg.setText("Speed " + round(speed,3,BigDecimal.ROUND_HALF_UP) + " kmph \n time " +(totalSec/1000) + " sec");
                }

            }else{
                totalSec = 0;
            }
            mapView.invalidate();
            prevTime = curTime;
        }
    }
}

确定每次调用此方法并使用新点更新地图时我都使用Geo_Class.distFrom(x1, y1, x2, y2)我的create方法计算两点之间的距离,并将新点设置为curr点和curr点将分配到prev点。以相同的方式计算总时间。并使用此

找到速度
speed = total distance/total time

答案 2 :(得分:1)

如果时间轴中有一组lat / lon数据,则可以通过该时间线中每组之间的距离总和来计算总距离。

我不知道你用什么公式计算距离,但看看here很好。

这是一个非常明智的话题,因为你在计算类似于某物的表面上的距离......我怎么能说......桃子。

说明这个想法:

  • 01:00 pm = 50 21 50N,004 09 25W
  • 05:00 pm = 45 21 50N,008 09 25W
  • 晚上10:00 = 42 21 04N,009 02 27W

总时间:9小时

总距离:(a-> b + b-> c)= 630km + 342.4km = 972.4km