マピオンAPI


Android Maps APIドキュメント

ピンチでアニメーション縮尺変更を可能にする(Android2.0以上)

public class MainActivity extends MapActivity {

	MapView mapView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mapView = new MapView(this, "APIキー");
		mapView.setClickable(true);
		setContentView(mapView);
		
		Overlay pinchOperator = new PinchAnimeOperatorOverlay();
		mapView.getOperatorOverlays().add(pinchOperator);
	}
}
public class PinchAnimeOperatorOverlay extends Overlay {

	private int pinchFirst;
	private int pinchPlus;
	
	private boolean begin;
	
	@Override
	public boolean onTouchEvent(MotionEvent e, MapView mapView) {
		int pointerCount = e.getPointerCount();
		if (pointerCount > 1) {
			int x0 = (int) e.getX(0);
			int y0 = (int) e.getY(0);
			
			int x1 = (int) e.getX(1);
			int y1 = (int) e.getY(1);
			
			final int action = e.getAction();
			switch (action & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN: {
				break;
			}
			case MotionEvent.ACTION_MOVE: {
				begin = true;
				
				int x2 = Math.abs(x0 - x1);
				int y2 = Math.abs(y0 - y1);
				int c = (int) Math.sqrt(x2 * x2 + y2 * y2);

				if (pinchFirst == 0) {
					pinchFirst = c;
				} else {
					pinchPlus = c;
				}

				break;
			}
			case MotionEvent.ACTION_UP: {
				break;
			}
			case MotionEvent.ACTION_CANCEL: {
				break;
			}
			case MotionEvent.ACTION_POINTER_UP: {
				if (pinchPlus != 0) {
					int pinch = pinchPlus - pinchFirst;
					if (pinch > 100) {
						int z = mapView.getZoomLevel();
						if (z < mapView.getMaxZoomLevel()) {
							mapView.getController().setZoom(z + 1);
						}
					} else if (pinch < -100) {
						int z = mapView.getZoomLevel();
						if (z > 1) {
							mapView.getController().setZoom(z - 1);
						}
					}
				}
				begin = false;
				pinchFirst = 0;
				break;
			}
			}
			return true;
		}
		return false;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean flag) {
		float scale = 1;
		if (begin && pinchPlus != 0) {
			int pinch = pinchPlus - pinchFirst;
			scale = 1f + pinch / 850f;
			int zoom = mapView.getZoomLevel();
			if (scale < 1) {
				if (zoom == 1) {
					scale = 1f;
				}
			} else {
				if (zoom == mapView.getMaxZoomLevel()) {
					scale = 1f;
				}
			}
		}
		canvas.scale(scale, scale, ViewRectangle.getViewWidth() / 2, ViewRectangle.getViewHeight() / 2);
	}
}