簡體   English   中英

如何在谷歌地圖Android API創建路線

[英]How to create a route at google maps Android API

我使用SupportMapFragment創建一個簡單的地圖應用程序,現在想在其上顯示一個路線。 GoogleSamples只有一個例子,它用Polygon繪制一條線,但它沒有創建正確的方法,只有兩點之間的簡單線:(如何在街道和道路上制作嚴格的路線?

遺憾的是,無法使用Google Maps API v2自動創建點到點之間的路線。 但是,您可以使用Google方向API: https//developers.google.com/maps/documentation/directions/

結果,您將能夠自己繪制路線。

如果有任何錯誤而不是抱歉......我不確定,但這種方法會幫助你......

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM