2012-03-20 1 views
0

안녕하세요 오버레이를 사용하여 MapView의 맨 위에 선을 그릴 응용 프로그램을 만들고 있습니다. 지금은 projection.toPixel() 메소드를 사용하고자하는 GPS 포인트를 설정했습니다. 이 메서드는 오류를 발생시키지 않지만 맵을 이동할 때 점이 고정되어 있지 않습니다. 제가 누락 된 것이 있습니까?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); 
} 

오버레이 클래스 방법을 그릴 :

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

을 MapView.getProjection (의 문서에서) 방법. "현재 상태에서 맵의 투사 당신은 더 많은 것을 위해이 객체에 개최 안 지도의 투사가 바뀔 수 있기 때문에 한 무승부보다. "

지도를 이동할 때 이전 픽셀 위치를 계산 한 투영이 더 이상 유효하지 않으므로 픽셀 위치가 더 이상 유효하지 않으므로 픽셀 위치를 다시 계산해야합니다.

+0

나는 당신이 말하는 것을 이해하고 있다고 생각하지만 projection.toPixels()을 호출 할 때마다 포인트를 얻습니다. 선은 그 위치에 머 무르지 않습니다. 오버레이 클래스 밖에서 위치를 잡아 내고 있기 때문일 수 있다고 생각했습니다. –

관련 문제