onCreate
onCreate

Reputation: 775

How to create a route at google maps Android API

i create a simple map application with SupportMapFragment and now want to display a route on it. GoogleSamples have only one example, it draws a line with Polygon, but it not create a right way, only simple line between two points :( How to make rigth route throught streets and roads?

Upvotes: 0

Views: 5820

Answers (2)

parthpatibandha
parthpatibandha

Reputation: 92

IF any error than sorry... I dont have sure idea but this types of methods will helps you...

    public class RouteOverlay extends Overlay {
    /** GeoPoints representing this routePoints. **/
    private final List<GeoPoint> routePoints;
    /** Colour to paint routePoints. **/
    private int colour;
    /** Alpha setting for route overlay. **/
    private static final int ALPHA = 120;
    /** Stroke width. **/
    private static final float STROKE = 4.5f;
    /** Route path. **/
    private final Path path;
    /** Point to draw with. **/
    private final Point p;
    /** Paint for path. **/
    private final Paint paint;


    /**
     * Public constructor.
     * 
     * @param route Route object representing the route.
     * @param defaultColour default colour to draw route in.
     */

    public RouteOverlay(final Route route, final int defaultColour) {
            super();
            routePoints = route.getPoints();
            colour = defaultColour;
            path = new Path();
            p = new Point();
            paint = new Paint();
    }

    @Override
    public final void draw(final Canvas c, final MapView mv,
                    final boolean shadow) {
            super.draw(c, mv, shadow);

            paint.setColor(colour);
            paint.setAlpha(ALPHA);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(STROKE);
            paint.setStyle(Paint.Style.STROKE);

            redrawPath(mv);
            c.drawPath(path, paint);
    }

    /**
     * Set the colour to draw this route's overlay with.
     * 
     * @param c  Int representing colour.
     */
    public final void setColour(final int c) {
            colour = c;
    }

    /**
     * Clear the route overlay.
     */
    public final void clear() {
            routePoints.clear();
    }

    /**
     * Recalculate the path accounting for changes to
     * the projection and routePoints.
     * @param mv MapView the path is drawn to.
     */

    private void redrawPath(final MapView mv) {
            final Projection prj = mv.getProjection();
            path.rewind();
            final Iterator<GeoPoint> it = routePoints.iterator();
            prj.toPixels(it.next(), p);
            path.moveTo(p.x, p.y);
            while (it.hasNext()) {
                    prj.toPixels(it.next(), p);
                    path.lineTo(p.x, p.y);
            }
            path.setLastPoint(p.x, p.y);
    }

}

Upvotes: 0

Aur&#233;lien Guillard
Aur&#233;lien Guillard

Reputation: 1203

Unfortunately it's not possible to create a route between to points automatically with Google Maps API v2. But, what you can do, it's to use the Google Direction API : https://developers.google.com/maps/documentation/directions/

And with the result, you will be able to draw a route by yourself.

Upvotes: 2

Related Questions