简体   繁体   中英

How to draw route using multiple geopoints?

I am implementing an android application for Map activity.

I am using Location listener for getting location updates. After updating location i am saving those updated latitude and longitude values in database.

I am able draw Route path From Source to destination using source latitude, longitude and destination latitude, longitude values. But using multiple latitude and longitude values of database at a time i want to draw the route path. I am using my RoutePath.java class to draw the route path for multiple latitudes and longitudes .Using those list of latitudes and longitudes i am able to draw the path, but it shows point to point stright line not shows the route path. See the below image...

在此处输入图片说明

If you observe carefully some points are on the route and some points are outside of route path. See again below image with full zooming...

在此处输入图片说明

Please help me if anyone knows the solution for this problem...

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;
        }
    }

您获得了直线,因为GeoPoints只是这样。.....意味着,如果您具有完整的路线,则它将根据您的Geopoints显示。...这都取决于GeoPoints。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM