2012-02-23 74 views
0

我開發了一個可以旋轉方向視圖的地圖,取決於用戶所在的位置(例如,如果用戶轉入南方,地圖將旋轉至南方,如指南針)。我已經嘗試過這個項目,並在幾周內取得了最小的成功。我在eclipse上沒有錯誤消息,但是當我調試代碼時,應用程序總是崩潰。 謝謝,我真的很需要的所有幫助在調試時旋轉Mapview錯誤

private class RotateView extends ViewGroup { 

    private Matrix mMatrix = new Matrix(); 
    private float[] mTemp = new float [2]; 
    private static final float SQ2 = 1.414213562373095f; 


    public RotateView(Context context) { 
     super(context); 

     // TODO Auto-generated constructor stub 
    } 



    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 

     canvas.save(Canvas.MATRIX_SAVE_FLAG); 
     canvas.rotate(-mHeading,getWidth()*0.5f , getHeight()*0.5f); 
     canvas.getMatrix(mMatrix); 
     super.dispatchDraw(canvas); 
     canvas.restore(); 
    } 



    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     // TODO Auto-generated method stub 
     final int width = getWidth(); 
     final int height = getHeight(); 
     final int count = getChildCount(); 
     for (int i=0; i<=count; i++){ 
      final View view = getChildAt(i); 
      final int childWidth = view.getMeasuredWidth(); 
      final int childHeight = view.getMeasuredHeight(); 
      final int childLeft = ((width - childWidth)/2); 
      final int childTop = ((height-childHeight)/2); 
      view.layout(childLeft, childTop, childLeft+childWidth, childTop+childHeight); 
     } 
    } 


    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     // TODO Auto-generated method stub 
     int mW = getDefaultSize(getMeasuredWidth(), widthMeasureSpec); 
     int mH = getDefaultSize(getMeasuredHeight(), heightMeasureSpec); 
     int sizeSpec; 
     if(mW > mH){ 
      sizeSpec = MeasureSpec.makeMeasureSpec((int)(mW*SQ2), MeasureSpec.EXACTLY); 
     } else { 
      sizeSpec = MeasureSpec.makeMeasureSpec((int) (mH*SQ2), MeasureSpec.EXACTLY); 
     } 
     final int count = getChildCount(); 
     for(int i = 0; i<count; i++){ 
      getChildAt(i).measure(sizeSpec, sizeSpec); 
     } 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent e) { 
     // TODO Auto-generated method stub 

     return super.dispatchTouchEvent(e); 
    } 

} 
// End of class RotateView 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    sensormanager =(SensorManager)getSystemService(SENSOR_SERVICE); 
    rotateview = new RotateView(this); 
    map = new MapView (this,"Map Key"); 
    rotateview.addView(map); 
    setContentView(rotateview); 
    controll(); 
} 

public void controll() { 

     controller = map.getController(); 
     //Zoom 
     map.setBuiltInZoomControls(true); 
     controller.setZoom(19); 
     overlayList = map.getOverlays(); 
     map.setClickable(true); 
     map.setEnabled(true); 
     //compass 
     compass = new MyLocationOverlay(MapIpbActivity.this, map); 
     overlayList.add(compass); 
     myLocation(); 
     Touchy t = new Touchy(); 
     overlayList.add(t); 
} 

public void myLocation() { 
    ourLocation = new MyLocationOverlay(MapIpbActivity.this, map); 
    map.getOverlays().add(ourLocation); 
    ourLocation.runOnFirstFix(new Runnable() { 
     public void run(){ 
      controller.animateTo(ourLocation.getMyLocation()); 
     } 

    }); 
    } 



@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    compass.disableCompass(); 
    ourLocation.disableMyLocation(); 
    sensormanager.unregisterListener(orientListener); 
    super.onPause(); 
} 
@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    compass.enableCompass(); 
    ourLocation.enableMyLocation(); 
    sensormanager.registerListener(orientListener, 
      sensormanager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
      sensormanager.SENSOR_DELAY_NORMAL); 

} 

final SensorEventListener orientListener = new SensorEventListener() { 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     // TODO Auto-generated method stub 
     if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){ 
      mHeading = event.values[0]; 
      rotateview.invalidate(); 
     } 
    } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
     // TODO Auto-generated method stub 

    } 
}; 
    class Touchy extends Overlay { 
     long start; 
     long stop; 
     int x; 
     int y; 
     GeoPoint TouchedPoint; 

     @Override 
     public boolean onTouchEvent(MotionEvent e, MapView m) { 
      // TODO Auto-generated method stub 
      if(e.getAction() == MotionEvent.ACTION_DOWN) { 
       start = e.getEventTime(); 
       x = (int) e.getX(); 
       y = (int) e.getY(); 
       TouchedPoint = map.getProjection().fromPixels(x, y); 
      } 
      if (e.getAction() == MotionEvent.ACTION_UP) { 
       stop = e.getEventTime(); 
      }if (stop - start > 2000){ 
        box(); 
        return true; 
      } 
      return false; 
     } 
+0

解決了這個問題,只需將i <= count更改爲i user1226997 2012-02-24 08:11:42

回答

0

解決了這個,只是改變i<=counti<count,這樣就可以了!但我的變焦和指南針沒有顯示。