如何使用多个地理位置绘制路线?

时间:2012-09-24 12:25:44

标签: android google-maps

我正在为Map活动实现一个Android应用程序。

我正在使用位置监听器来获取位置更新。更新位置后,我将这些更新的纬度和经度值保存在数据库中。

我可以使用源纬度,经度和目的地纬度,经度值绘制从源到目的地的路径路径。但是我想一次使用数据库的多个纬度和经度值来绘制路径路径。我正在使用 RoutePath.java 类来绘制多个纬度和经度的路径路径。使用那些纬度和经度列表,我能够绘制路径,但它显示点到点直线不显示路径路径。见下图...

enter image description here

如果您仔细观察路线上的某些点,并且某些点位于路线路径之外。完整缩放后再次查看下图...

enter image description here

如果有人知道这个问题的解决方案,请帮助我......

RoutePath.java:

public class RoutePath extends Overlay {

    private int _pathColor;
    private final List<GeoPoint> _points;
    private boolean _drawStartEnd;

    public RoutePath(List<GeoPoint> points) {
        this(points, Color.RED, true);
    }

    public RoutePath(List<GeoPoint> points, int pathColor,
            boolean drawStartEnd) {
        _points = points;
        _pathColor = pathColor;
        _drawStartEnd = drawStartEnd;
    }

    private void drawOval(Canvas canvas, Paint paint, Point point) {
        Paint ovalPaint = new Paint(paint);
        ovalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        ovalPaint.setStrokeWidth(2);
        int _radius = 6;
        RectF oval = new RectF(point.x - _radius, point.y - _radius, point.x
                + _radius, point.y + _radius);
        canvas.drawOval(oval, ovalPaint);
    }

    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        Projection projection = mapView.getProjection();
        if (shadow == false && _points != null) {
            Point startPoint = null, endPoint = null;
            Path path = new Path();
            // We are creating the path
            for (int i = 0; i < _points.size(); i++) {
                GeoPoint gPointA = _points.get(i);
                Point pointA = new Point();
                projection.toPixels(gPointA, pointA);
                if (i == 0) { // This is the start point
                    startPoint = pointA;
                    path.moveTo(pointA.x, pointA.y);
                } else {
                    if (i == _points.size() - 1)// This is the end point
                        endPoint = pointA;
                    path.lineTo(pointA.x, pointA.y);
                }
            }

            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(_pathColor);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(5);
            paint.setAlpha(90);
            if (getDrawStartEnd()) {
                if (startPoint != null) {
                    drawOval(canvas, paint, startPoint);
                }
                if (endPoint != null) {
                    drawOval(canvas, paint, endPoint);
                }
            }
            if (!path.isEmpty())
                canvas.drawPath(path, paint);
        }
        return super.draw(canvas, mapView, shadow, when);
    }

    public boolean getDrawStartEnd() {
        return _drawStartEnd;
    }

    public void setDrawStartEnd(boolean markStartEnd) {
        _drawStartEnd = markStartEnd;
        }
    }

1 个答案:

答案 0 :(得分:0)

你有直线,因为GeoPoints只是那样.....意味着如果你有一个完整的路径路径,那么它将根据你的Geopoints显示....这一切都取决于GeoPoints。!