Skip to content

Commit

Permalink
Show miles at the bottom #5
Browse files Browse the repository at this point in the history
  • Loading branch information
pengrad committed Dec 10, 2017
1 parent dabfff9 commit 0b3925a
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ class MapScaleModel {

private final float density;
private int maxWidth;
private boolean isMiles = false;
private int[] distances = METERS;
private double tileSizeAtZoom0 = TILE_SIZE_METERS_AT_0_ZOOM;

private float lastZoom = -1;
private double lastLatitude = -100;
Expand All @@ -27,31 +24,26 @@ class MapScaleModel {
this.density = density;
}

Scale setMaxWidth(int width) {
void setMaxWidth(int width) {
maxWidth = width;
return update(lastZoom, lastLatitude);
}

Scale setIsMiles(boolean miles) {
isMiles = miles;

if (miles) {
tileSizeAtZoom0 = TILE_SIZE_FT_AT_0_ZOOM;
distances = FT;
} else {
tileSizeAtZoom0 = TILE_SIZE_METERS_AT_0_ZOOM;
distances = METERS;
}

return update(lastZoom, lastLatitude);
void setPosition(float zoom, double latitude) {
lastZoom = zoom;
lastLatitude = latitude;
}

/**
* See http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Resolution_and_Scale
*/
Scale update(final float zoom, final double latitude) {
Scale update(boolean meters) {
float zoom = lastZoom;
double latitude = lastLatitude;
if (zoom < 0 || Math.abs(latitude) > 90) return null;

double tileSizeAtZoom0 = meters ? TILE_SIZE_METERS_AT_0_ZOOM : TILE_SIZE_FT_AT_0_ZOOM;
int[] distances = meters ? METERS : FT;

final double resolution = tileSizeAtZoom0 / density * Math.cos(latitude * Math.PI / 180) / Math.pow(2, zoom);

int distance = 0;
Expand All @@ -65,16 +57,16 @@ Scale update(final float zoom, final double latitude) {

lastZoom = zoom;
lastLatitude = latitude;
return new Scale(text(distance), (float) screenDistance);
return new Scale(text(distance, meters), (float) screenDistance);
}

private String text(int distance) {
if (isMiles) {
if (distance < FT_IN_MILE) return distance + " ft";
else return distance / FT_IN_MILE + " mi";
} else {
private String text(int distance, boolean meters) {
if (meters) {
if (distance < 1000) return distance + " m";
else return distance / 1000 + " km";
} else {
if (distance < FT_IN_MILE) return distance + " ft";
else return distance / FT_IN_MILE + " mi";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ public class MapScaleView extends View {
private float textHeight;
private float horizontalLineY;

private Scale scale;
private Scales scales;

private ScaleType scaleType = ScaleType.BOTH;

private enum ScaleType {
METERS_ONLY, MILES_ONLY, BOTH
}

public MapScaleView(Context context) {
this(context, null);
Expand All @@ -37,7 +43,9 @@ public MapScaleView(Context context, AttributeSet attrs, int defStyleAttr) {

float density = getResources().getDisplayMetrics().density;
mapScaleModel = new MapScaleModel(density);
mapScaleModel.setIsMiles(viewConfig.isMiles);
if (viewConfig.isMiles) {
scaleType = ScaleType.MILES_ONLY;
}

desiredWidth = viewConfig.desiredWidth;
int defaultColor = viewConfig.color;
Expand Down Expand Up @@ -79,13 +87,48 @@ public void setStrokeWidth(float strokeWidth) {
invalidate();
}

/**
* @deprecated Use milesOnly()
*/
@Deprecated
public void setIsMiles(boolean miles) {
scale = mapScaleModel.setIsMiles(miles);
invalidate();
if (miles) milesOnly();
else metersAndMiles();
}

public void metersOnly() {
scaleType = ScaleType.METERS_ONLY;
updateScales();
}

public void milesOnly() {
scaleType = ScaleType.MILES_ONLY;
updateScales();
}

public void metersAndMiles() {
scaleType = ScaleType.BOTH;
updateScales();
}

public void update(float zoom, double latitude) {
scale = mapScaleModel.update(zoom, latitude);
mapScaleModel.setPosition(zoom, latitude);
updateScales();
}

private void updateScales() {
Scale top, bottom = null;

if (scaleType == ScaleType.MILES_ONLY) {
top = mapScaleModel.update(false);
} else {
top = mapScaleModel.update(true);
if (scaleType == ScaleType.BOTH) {
bottom = mapScaleModel.update(false);
}
}

scales = new Scales(top, bottom);
invalidate();
}

Expand All @@ -94,7 +137,8 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureDimension(desiredWidth(), widthMeasureSpec);
int height = measureDimension(desiredHeight(), heightMeasureSpec);

scale = mapScaleModel.setMaxWidth(width);
mapScaleModel.setMaxWidth(width);
updateScales();

setMeasuredDimension(width, height);
}
Expand All @@ -104,7 +148,7 @@ private int desiredWidth() {
}

private int desiredHeight() {
return (int) (paint.getTextSize() * 1.5 + paint.getStrokeWidth());
return (int) (paint.getTextSize() * 3 + paint.getStrokeWidth());
}

private int measureDimension(int desiredSize, int measureSpec) {
Expand All @@ -122,17 +166,34 @@ private int measureDimension(int desiredSize, int measureSpec) {

@Override
public void onDraw(Canvas canvas) {
if (scale == null) return;
if (scales == null || scales.top() == null) {
return;
}

final String text = scale.text();
final float lineLength = scale.length();
Scale top = scales.top();

canvas.drawText(text, 0, textHeight, paint);
canvas.drawText(top.text(), 0, textHeight, paint);

strokePath.rewind();
strokePath.moveTo(0, horizontalLineY);
strokePath.lineTo(lineLength, horizontalLineY);
strokePath.lineTo(lineLength, textHeight);
strokePath.lineTo(top.length(), horizontalLineY);
strokePath.lineTo(top.length(), textHeight);

Scale bottom = scales.bottom();
if (bottom != null) {

if (bottom.length() > top.length()) {
strokePath.moveTo(top.length(), horizontalLineY);
strokePath.lineTo(bottom.length(), horizontalLineY);
} else {
strokePath.moveTo(bottom.length(), horizontalLineY);
}

strokePath.lineTo(bottom.length(), textHeight * 2);

float bottomTextY = horizontalLineY + textHeight + textHeight / 2;
canvas.drawText(bottom.text(), 0, bottomTextY, paint);
}

canvas.drawPath(strokePath, strokePaint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public String text() {
public float length() {
return length;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.pengrad.mapscaleview;

class Scales {
private final Scale top, bottom;

Scales(Scale top, Scale bottom) {
this.top = top;
this.bottom = bottom;
}

public Scale top() {
return top;
}

public Scale bottom() {
return bottom;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ public void changeMiles(View view) {
scaleView.setIsMiles(isMiles);
}

private boolean isMeters = false;

public void changeMeters(View view) {
isMeters = !isMeters;
if (isMeters) scaleView.metersOnly();
else scaleView.metersAndMiles();
}

public void changeSize(View view) {
ViewGroup.LayoutParams layoutParams = scaleView.getLayoutParams();
layoutParams.width = new Random().nextInt(200) + 200;
Expand Down
6 changes: 6 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
android:onClick="changeMiles"
android:text="miles"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeMeters"
android:text="meters"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 0b3925a

Please sign in to comment.