2012-03-20 46 views
0

您好我正在创建一个应用程序,它将使用叠加在MapView的顶部绘制线条。目前我已经设置了GPS点,我想使用projection.toPixel()方法。该方法不会引发任何错误,但在地图移动时,点不会固定在地图上。有什么我失踪?如何将GPS点转换为Overlay的屏幕像素?

//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()。线不停留在那里的位置。我想这可能是因为我得到Overlay课程以外的职位。 – 2012-03-20 18:24:49

相关问题