1

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

4

3 回答 3

6

我会创建一个名为航点的类

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));
于 2011-09-13T23:17:40.750 回答
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);

现在,当用户单击按钮以记录轨迹并找到总距离和时间时,我已经绘制了路径地图活动并存储到 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)我的创建方法,该方法计算两点之间的距离,当将新点设置为当前点时,当前点将分配给上一个观点。时间计算总时间的方法相同。并找到使用它的速度

speed = total distance/total time
于 2011-09-14T12:47:15.163 回答
1

如果您在时间线上确实有一组纬度/经度数据,您可以通过该时间线中每个集合之间的距离之和来计算总距离。

我不知道您使用哪个公式来计算距离,但很高兴看看这里

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

为了说明这个想法:

  • 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

于 2011-09-13T19:24:51.873 回答