マピオンAPI


Android Maps APIドキュメント

独自縮尺変更ボタンを作る

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 zoomDownOverlay = new ZoomDownOverlay(this);
		mapView.getOverlays().add(zoomDownOverlay);
		
		Overlay zoomUpOverlay = new ZoomUpOverlay(this);
		mapView.getOverlays().add(zoomUpOverlay);
	}
}
public class ZoomDownOverlay extends Overlay {

	private Context context;
	
	private Bitmap currentButton;
	private Bitmap normal;
	private Bitmap disabled;
	private Bitmap pressed;
	
	private Point before = new Point();
	
	public ZoomDownOverlay(Context context) {
		this.context = context;
		normal = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.btn_zoom_down_normal);
		disabled = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.btn_zoom_down_disabled);
		pressed = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.btn_zoom_down_pressed);
		currentButton = normal;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		if (!shadow) {
			int viewWidth = mapView.getWidth();
			int viewHeight = mapView.getHeight();
			int bitmapWidth = currentButton.getWidth();
			int bitmapHeight = currentButton.getHeight();
			int marginright = 5;
			int marginbottom = 0;
			
			canvas.restore();
			Bitmap button = mapView.getZoomLevel() == 1 ?
					disabled : currentButton;
			
			int x = viewWidth - bitmapWidth * 2 - marginright;
			int y = viewHeight - bitmapHeight / 2 - marginbottom;
			canvas.drawBitmap(button, x, y, null);
			canvas.save();
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e, MapView mapView) {
		Point p = new Point((int) e.getX(), (int) e.getY());
		if (inside(p, mapView)) {
			switch (e.getAction()) {
			case MotionEvent.ACTION_DOWN:
				before = p;
				currentButton = pressed;
				return false;
			case MotionEvent.ACTION_UP:
				currentButton = normal;
				if (distance(p) < 20) {
					zoom(mapView);
				}
				return true;
			case MotionEvent.ACTION_MOVE:
				return false;
			case MotionEvent.ACTION_CANCEL:
				return false;
			}
		} else {
			switch (e.getAction()) {
			case MotionEvent.ACTION_UP:
				currentButton = normal;
				break;
			}
		}
		return false;
    }
	
	private boolean inside(Point p, MapView mapView) {
		Bitmap btnZoomDown = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.btn_zoom_down_normal);
		if (btnZoomDown == null) {
			return false;
		}
		
		MapUtil.rotateItemFromCenter(mapView.getDegrees(), p);
		
		int viewWidth = mapView.getWidth();
		int viewHeight = mapView.getHeight();
		int bitmapWidth = btnZoomDown.getWidth();
		int bitmapHeight = btnZoomDown.getHeight();

		int minx = viewWidth - bitmapWidth * 2;
		int maxx = viewWidth - bitmapWidth;
		int miny = viewHeight - bitmapHeight;
		int maxy = viewHeight;
		if (p.x >= minx && p.x <= maxx && p.y >= miny && p.y <= maxy) {
			return true;
		}
		return false;
	}
	
	private int distance(Point after) {
		int x = Math.abs(before.x - after.x);
		int y = Math.abs(before.y - after.y);
		return x + y;
	}
	
	private void zoom(MapView mapView) {
		int zoom = mapView.getZoomLevel();
		int zoom2 = zoom - 1;
		if (zoom2 > 0) {
			mapView.getController().setZoom(zoom2);
    	}
	}
	
	@Override
	public String toString() {
		return getClass().getName();
	}
}
public class ZoomUpOverlay extends Overlay {

	private Context context;
	
	private Bitmap currentButton;
	private Bitmap normal;
	private Bitmap disabled;
	private Bitmap pressed;
	
	private Point before = new Point();
	
	public ZoomUpOverlay(Context context) {
		this.context = context;
		normal = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.btn_zoom_up_normal);
		disabled = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.btn_zoom_up_disabled);
		pressed = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.btn_zoom_up_pressed);
		currentButton = normal;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		if (!shadow) {
			int viewWidth = mapView.getWidth();
			int viewHeight = mapView.getHeight();
			int bitmapWidth = currentButton.getWidth();
			int bitmapHeight = currentButton.getHeight();
			int marginright = 5;
			int marginbottom = 0;
			
			canvas.restore();
			Bitmap button = mapView.getZoomLevel() ==
				mapView.getMaxZoomLevel() ?
						disabled : currentButton;

			int x = viewWidth - bitmapWidth - marginright;
			int y = viewHeight - bitmapHeight / 2 - marginbottom;
			canvas.drawBitmap(button, x, y, null);
			canvas.save();
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e, MapView mapView) {
		Point p = new Point((int) e.getX(), (int) e.getY());
		if (inside(p, mapView)) {
			switch (e.getAction()) {
			case MotionEvent.ACTION_DOWN:
				before = p;
				currentButton = pressed;
				return false;
			case MotionEvent.ACTION_UP:
				currentButton = normal;
				if (distance(p) < 20) {
					zoom(mapView);
				}
				return true;
			case MotionEvent.ACTION_MOVE:
				return false;
			case MotionEvent.ACTION_CANCEL:
				return false;
			}
		} else {
			switch (e.getAction()) {
			case MotionEvent.ACTION_UP:
				currentButton = normal;
				break;
			}
		}
        return false;
    }
	
	private boolean inside(Point p, MapView mapView) {
		Bitmap btnZoomUp = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.btn_zoom_up_normal);
		if (btnZoomUp == null) {
			return false;
		}
		
		MapUtil.rotateItemFromCenter(mapView.getDegrees(), p);
		
		int viewWidth = mapView.getWidth();
		int viewHeight = mapView.getHeight();
		int bitmapWidth = btnZoomUp.getWidth();
		int bitmapHeight = btnZoomUp.getHeight();
		
		int minx = viewWidth - bitmapWidth;
		int maxx = viewWidth;
		int miny = viewHeight - bitmapHeight;
		int maxy = viewHeight;
		if (p.x >= minx && p.x <= maxx && p.y >= miny && p.y <= maxy) {
			return true;
		}
		return false;
	}
	
	private int distance(Point after) {
		int x = Math.abs(before.x - after.x);
		int y = Math.abs(before.y - after.y);
		return x + y;
	}
	
	private void zoom(MapView mapView) {
		int zoom = mapView.getZoomLevel();
		int zoom2 = zoom + 1;
		if (zoom2 <= mapView.getMaxZoomLevel()) {
			mapView.getController().setZoom(zoom2);
    	}
	}
	
	@Override
	public String toString() {
		return getClass().getName();
	}
}