2012-10-14 92 views
0

我正試圖在Android地圖上使用Google地圖引擎覆蓋地圖上的不規則多邊形。Android帆布:填充時多邊形會變形(Paint.Style.Fill)?

這是一個示例代碼,我正在測試...覆蓋Overlay類的繪製方法。

@Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
      long when) { 

     ArrayList<GeoPoint> points = new ArrayList<GeoPoint>(); 

     points.add(getPoint(40.69601721120307,-7.975247972597344)); 
     points.add(getPoint(39.59209428101375,-5.602198679879018)); 
     points.add(getPoint(37.98114679179523,-5.43877491468928)); 
     points.add(getPoint(39.01059159609532,-4.050442951443798)); 
     points.add(getPoint(37.99556954318146,-1.728440918734253)); 
     points.add(getPoint(39.26565517073047,-2.576421100521262)); 
     points.add(getPoint(40.21994329403012,-1.164105822610836)); 
     points.add(getPoint(40.57540056971796,-3.415060042751821)); 
     points.add(getPoint(42.5081775151286,-4.553753858729792)); 
     points.add(getPoint(40.74284989396188,-5.388244948956315)); 
     points.add(getPoint(40.69601721120307,-7.975247972597344)); 



     Paint mPaint = new Paint(); 
     mPaint.setColor(Color.RED);   
     mPaint.setStyle(Style.STROKE); 
     mPaint.setStrokeWidth(2); 

     Point point1 = null; 
     Path path = new Path(); 
     for(GeoPoint p : points){                     

      Point point2 = new Point();   
      mapView.getProjection().toPixels(p, point2); 

      if(point1 != null){     
       path.lineTo(point2.x, point2.y);     
       canvas.drawPath(path, mPaint); 
       mapView.getProjection().toPixels(p, point1); 
      } else { 
       point1 = new Point(); 
       mapView.getProjection().toPixels(p, point1);     
       path.moveTo(point1.x, point1.y); 
      } 

     } 
     return true; 
    } 

當我運行測試,我可以在地圖上看到這樣的結果:

enter image description here

然而,當我改變風格Style.STROKE_FILL或Style.FILL我得到的下面上圖:

enter image description here

我發現在論壇上沒有解決這個問題。

我將不勝感激任何幫助。

謝謝。

回答

2

試試這個。改變是在路徑完成後繪製路徑。

Point point1 = null; 
    Path path = new Path(); 

    for(GeoPoint p : points){                     

     Point point2 = new Point();   
     mapView.getProjection().toPixels(p, point2); 

     if(point1 != null){     
      path.lineTo(point2.x, point2.y);       
      mapView.getProjection().toPixels(p, point1); 
     } else { 
      point1 = new Point(); 
      mapView.getProjection().toPixels(p, point1);     
      path.moveTo(point1.x, point1.y); 
     } 

    } 
    canvas.drawPath(path, mPaint); 

您還可以在繪製之前檢查路徑。

+0

謝謝soooo多! :) –

+0

它工作?如果有的話,請接受答案。 – Simon

0

確定點的順序是正確的嗎?

0
 Paint mPaint = new Paint(); 
     Path path = new Path(); 


     // 
     GeoPoint name0 = new GeoPoint(40696017,-7975247); // = 40.69601721120307,-7.975247972597344 
     Point pname0 = new Point(); 
     projection.toPixels(name0, pname0); 
     path.moveTo(pname0.x, pname0.y);  //first= moveto 
     // 
     GeoPoint name1 = new GeoPoint(39592094,-5602198); // 39.59209428101375,-5.602198679879018 
     Point pname1 = new Point(); 
     projection.toPixels(name1, pname1); 
     path.lineTo(pname1.x, pname1.y); 
     // 
     GeoPoint name2 = new GeoPoint(...,...); 
     Point pname2 = new Point(); 
     projection.toPixels(name2, pname2); 
     path.lineTo(pname2.x, pname2.y); 

     // ... , ... 


     canvas.drawPath(path, mPaint);