private static final int MAP_TILE_SIZE = 256;
/** x position (scale depends on zoom) */
- private long _xPosition = 0L;
+ private int _xPosition = 0;
/** y position (scale depends on zoom) */
- private long _yPosition = 0L;
+ private int _yPosition = 0;
- /** Zoom level, from 2 to 15 */
+ /** Zoom level, from 2 to max */
private int _zoom = 12;
/** Factor to zoom by, 2 to the power of zoom */
private int _zoomFactor = 1 << _zoom;
+ /** Maximum zoom level */
+ private static final int MAX_ZOOM = 21;
/**
double diffY = Math.abs(inMaxY - inMinY);
// Find out what zoom level to go to
int requiredZoom = -1;
- for (int currZoom = 15; currZoom >= 2; currZoom--)
+ for (int currZoom = MAX_ZOOM; currZoom >= 2; currZoom--)
{
if (transformToPixels(diffX, currZoom) < inWidth
&& transformToPixels(diffY, currZoom) < inHeight)
}
if (requiredZoom < 2) requiredZoom = 2;
// Set position
- _zoom = requiredZoom;
- _zoomFactor = 1 << _zoom;
+ setZoom(requiredZoom);
_xPosition = transformToPixels((inMinX + inMaxX) / 2.0);
_yPosition = transformToPixels((inMinY + inMaxY) / 2.0);
}
+ /**
+ * Ensure that zoom and zoomFactor remain in sync
+ * @param inZoom zoom level to set
+ */
+ private void setZoom(int inZoom)
+ {
+ _zoom = inZoom;
+ _zoomFactor = 1 << _zoom;
+ }
+
/**
* Zoom and pan to show the selected area
* @param inMinX minimum pixels X
// Find out what zoom level to go to
int requiredZoom = -1;
int multFactor = 0;
- for (int currZoom = 16; currZoom >= _zoom; currZoom--)
+ for (int currZoom = MAX_ZOOM; currZoom >= _zoom; currZoom--)
{
multFactor = 1 << (currZoom - _zoom);
if ((diffX * multFactor) < inWidth && (diffY * multFactor) < inHeight)
break;
}
}
- _zoom = requiredZoom;
- _zoomFactor = 1 << _zoom;
+ setZoom(requiredZoom);
// Set position
_xPosition = (_xPosition - inWidth/2 + (inMinX + inMaxX) / 2) * multFactor;
_yPosition = (_yPosition - inHeight/2 + (inMinY + inMaxY) / 2) * multFactor;
* @param inValue value to transform
* @return pixels
*/
- private long transformToPixels(double inValue)
+ private int transformToPixels(double inValue)
{
return transformToPixels(inValue, _zoom);
}
* @param inZoom zoom value to use
* @return pixels
*/
- private static long transformToPixels(double inValue, int inZoom)
+ private static int transformToPixels(double inValue, int inZoom)
{
- return (long) (inValue * MAP_TILE_SIZE * (1 << inZoom));
+ return (int) (inValue * MAP_TILE_SIZE * (1 << inZoom));
}
/**
*/
public int getXFromCentre(double inValue)
{
- return (int) (transformToPixels(inValue) - _xPosition);
+ return transformToPixels(inValue) - _xPosition;
}
/**
*/
public int getYFromCentre(double inValue)
{
- return (int) (transformToPixels(inValue) - _yPosition);
+ return transformToPixels(inValue) - _yPosition;
}
/**
* @param inPosition position of point
* @return tile index for that point
*/
- private int getTileIndex(long inPosition)
+ private int getTileIndex(int inPosition)
{
- return (int) (inPosition / MAP_TILE_SIZE);
+ return inPosition / MAP_TILE_SIZE;
}
/**
* @param inPosition position of point
* @return pixel offset for that point
*/
- private int getDisplayOffset(long inPosition)
+ private int getDisplayOffset(int inPosition)
{
- return (int) (inPosition % MAP_TILE_SIZE);
- // Maybe >> 8 would be slightly faster?
+ return inPosition % MAP_TILE_SIZE;
+ // I thought that &255 would be slightly faster, but it gives the wrong result
}
/**
*/
public void zoomIn()
{
- if (_zoom < 16)
+ if (_zoom < MAX_ZOOM)
{
- _zoom++;
- _zoomFactor = 1 << _zoom;
+ setZoom(_zoom + 1);
_xPosition *= 2;
_yPosition *= 2;
}
*/
public void zoomOut()
{
- if (_zoom >= 2)
+ if (_zoom >= 3)
{
- _zoom--;
- _zoomFactor = 1 << _zoom;
+ setZoom(_zoom - 1);
_xPosition /= 2;
_yPosition /= 2;
}