2012-02-23 16 views
0

方向ビューを回転できるマップを作成しました。ユーザーが直面する場所に依存します(たとえば、南に回っている場合、地図は南向きに回転します)。私はすでに数週間の間、最小の成功でこのプロジェクトを試してみました。私はEclipse上でエラーメッセージはありませんが、私はコードをデバッグすると、アプリケーションは常にクラッシュします。 おかげで、私は本当にこの、ちょうどi<counti<=countを変更し、すべてが動作しますが解決デバッグ時に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

答えて

0

すべての助けが必要!しかし、ズームとコンパスは表示されませんでした。

関連する問題