2012-03-20 1 views
0

こんにちは私は、オーバーレイを使用してMapViewの上に線を引くアプリケーションを作成しています。その瞬間、私はGPSポイントを、projection.toPixel()メソッドを使用したいと設定しました。この方法ではエラーは発生しませんが、マップを移動したときにポイントが固定されたままになることはありません。私が紛失しているものはありますか?GPSポイントをオーバーレイ付きスクリーンピクセルに変換するには?

//Zoom into certain area 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    MapController mc = mapView.getController(); 

    switch(keyCode) 
    { 
     case KeyEvent.KEYCODE_1: 
      mc.zoomIn(); 
      break; 

     case KeyEvent.KEYCODE_2: 
      mc.zoomOut(); 
      break; 

     case KeyEvent.KEYCODE_4: 
      //Create new path 
      path = new Path(); 

      //Get Location Data from GPS 

      p = new GeoPoint(
        (int) (54.9886454 * 1E6), 
        (int) (-7.522208 * 1E6)); 

      //Convert GPS location to Screen Pixels 
      Point screenPts1 = new Point(); 

      mapView.getProjection().toPixels(p, screenPts1); 

      //Start Path and pass locations 
      path.moveTo(screenPts1.x, screenPts1.y); 
      path.lineTo(screenPts1.x, screenPts1.y); 

      System.out.println("New Path "); 
      break; 

     case KeyEvent.KEYCODE_5:     
      //Get Location Data from GPS 

      p = new GeoPoint(
        (int) (54.9984931 * 1E6), 
        (int) (-7.522208 * 1E6)); 

      //Convert GPS location to Screen Pixels 
      Point screenPts2 = new Point(); 
      mapView.getProjection().toPixels(p, screenPts2); 

      //Pass locations to the path 
      path.lineTo(screenPts2.x, screenPts2.y); 

      System.out.println("Continue Path "); 
      break; 

     case KeyEvent.KEYCODE_6:     
      //Get Location Data from GPS 

      p = new GeoPoint(
        (int) (54.9994777 * 1E6), 
        (int) (-7.5005787 * 1E6)); 

      //Convert GPS location to Screen Pixels 
      Point screenPts3 = new Point(); 
      mapView.getProjection().toPixels(p, screenPts3); 

      //Pass locations to the path 
      path.lineTo(screenPts3.x, screenPts3.y); 

      //Close the path and add it to the _graphics array for it to be drawn 
      _graphics.add(path); 

      System.out.println("End Path "); 

      break; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

オーバーレイクラスメソッド描く:MapView.getProjection()メソッドのドキュメントから

public boolean draw(Canvas canvas, MapView mapView, boolean shadow,long when) 
    {  
     super.draw(canvas,mapView,shadow); 

     //-- Create new paint object -- 
     Paint mPaint = new Paint(); 
     mPaint.setDither(true); 
     mPaint.setColor(Color.RED); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(2); 

     //-- Take all the paths from the _graphics array and draw them to the Screen -- 
     for (Path path : _graphics) 
     { 
      canvas.drawPath(path, mPaint); 
     } 

     return true; 
    } 

答えて

1

を:「現在の状態でのマップの投影あなたはより多くのために、このオブジェクトを保持べきではありません地図の投影が変わる可能性があるので、1つのドローより

マップを移動したときに、以前のピクセル位置を計算した投影がもはや有効でなく、ピクセル位置がもはや有効でないため、ピクセル位置を再計算する必要があります。

+0

私はあなたが言っていることを理解していると思いますが、ポイントを得るたびにprojection.toPixels()を呼び出しています。ラインはその位置に留まらない。私はこれがOverlayクラス外のポジションを取っていることが原因であると考えていました。 –