マピオン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 PinchOperatorOverlay();
		mapView.getOperatorOverlays().add(pinchOperator);
	}
}
public class PinchOperatorOverlay extends Overlay {
	
	private int pinchFirst;
	private int pinchPlus;
	
	@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: {
				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: {				
				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);
					}
				}
				pinchFirst = 0;
				break;
			}
			}
			return true;
		}
		return false;
	}
}