2013-11-28 61 views
0

我使用SupportMapFragment創建了一個簡單的地圖應用程序,現在想要在其上顯示路線。 GoogleSamples只有一個例子,它借鑑與多邊形一條線,但它不能創造一個正確的方式,兩點:( 之間只有簡單的線條如何使分辯路線throught街道和道路?如何在Google地圖上創建路線Android API

回答

0

如果有任何錯誤比抱歉...我沒有確定的想法,但這種類型的方法將幫助你。 ..

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

} 
相關問題