diff options
| author | Konrad Gräfe <konradgraefe@aol.com> | 2015-01-26 21:24:13 +0100 |
|---|---|---|
| committer | rsudev <rasch@munin-soft.de> | 2015-02-09 23:12:19 +0100 |
| commit | 8fbaa36d29ea1934a48f51039bb60eaa1df3f010 (patch) | |
| tree | 89b8fb17d7b07a8410766fd1462e7842ddcfa754 /main/src | |
| parent | a63fddc36ad53af040482e1604ea96f7a8fe83c5 (diff) | |
| download | cgeo-8fbaa36d29ea1934a48f51039bb60eaa1df3f010.zip cgeo-8fbaa36d29ea1934a48f51039bb60eaa1df3f010.tar.gz cgeo-8fbaa36d29ea1934a48f51039bb60eaa1df3f010.tar.bz2 | |
Add distance overlay to the map view
Diffstat (limited to 'main/src')
9 files changed, 454 insertions, 69 deletions
diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 933fdbd..4724566 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -140,6 +140,7 @@ public class CGeoMap extends AbstractMap implements ViewFactory { private MapViewImpl mapView; private CachesOverlay overlayCaches; private PositionAndScaleOverlay overlayPositionAndScale; + private DistanceOverlay overlayDistance; final private GeoDirHandler geoDirUpdate = new UpdateLoc(this); private SearchResult searchIntent = null; @@ -454,6 +455,10 @@ public class CGeoMap extends AbstractMap implements ViewFactory { overlayPositionAndScale.setHistory(trailHistory); } + if (coordsIntent != null || geocodeIntent != null) { + overlayDistance = mapView.createAddDistanceOverlay(coordsIntent, geocodeIntent); + } + mapView.repaintRequired(null); setZoom(Settings.getMapZoom(mapMode)); @@ -1003,6 +1008,11 @@ public class CGeoMap extends AbstractMap implements ViewFactory { map.overlayPositionAndScale.setCoordinates(currentLocation); map.overlayPositionAndScale.setHeading(currentHeading); map.mapView.repaintRequired(map.overlayPositionAndScale); + + if (map.overlayDistance != null) { + map.overlayDistance.setCoordinates(currentLocation); + map.mapView.repaintRequired(map.overlayDistance); + } } } } catch (final RuntimeException e) { diff --git a/main/src/cgeo/geocaching/maps/DistanceOverlay.java b/main/src/cgeo/geocaching/maps/DistanceOverlay.java new file mode 100755 index 0000000..211db98 --- /dev/null +++ b/main/src/cgeo/geocaching/maps/DistanceOverlay.java @@ -0,0 +1,178 @@ +package cgeo.geocaching.maps; + +import cgeo.geocaching.CgeoApplication; +import cgeo.geocaching.DataStore; +import cgeo.geocaching.location.Geopoint; +import cgeo.geocaching.location.Units; +import cgeo.geocaching.location.Viewport; +import cgeo.geocaching.maps.interfaces.GeneralOverlay; +import cgeo.geocaching.maps.interfaces.MapProjectionImpl; +import cgeo.geocaching.maps.interfaces.MapViewImpl; +import cgeo.geocaching.maps.interfaces.OverlayImpl; + +import android.content.Context; +import android.graphics.BlurMaskFilter; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Point; +import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.Typeface; +import android.location.Location; +import android.util.DisplayMetrics; +import android.view.WindowManager; + +public class DistanceOverlay implements GeneralOverlay { + private Geopoint currentCoords; + private final Geopoint destinationCoords; + + private Paint paintBox = null; + private Paint paintBoxShadow = null; + private Paint paintText = null; + private Paint paintCompass = null; + private BlurMaskFilter blurBoxShadow = null; + + private final boolean needsInvertedColors; + private float pixelDensity = 0; + private final float boxWidth, boxHeight, boxCornerRadius, boxShadowSize, boxPadding; + private final float textHeight, maxTextWidth; + private final float boxX, boxY; + + private String distanceText = null; + + private OverlayImpl ovlImpl = null; + + public DistanceOverlay(final OverlayImpl ovlImpl, final MapViewImpl mapView, final Geopoint coords, final String geocode) { + this.ovlImpl = ovlImpl; + + if (coords == null) { + final Viewport bounds = DataStore.getBounds(geocode); + if (bounds == null) { + this.destinationCoords = new Geopoint(0, 0); + } else { + this.destinationCoords = bounds.center; + } + } else { + this.destinationCoords = coords; + } + + final DisplayMetrics metrics = new DisplayMetrics(); + final WindowManager windowManager = (WindowManager) CgeoApplication.getInstance().getSystemService(Context.WINDOW_SERVICE); + windowManager.getDefaultDisplay().getMetrics(metrics); + + pixelDensity = metrics.density; + + boxPadding = 2; + boxWidth = 100 * pixelDensity + 3 * boxPadding; + boxHeight = 30 * pixelDensity + 2 * boxPadding; + boxCornerRadius = 5 * pixelDensity; + boxShadowSize = 1 * pixelDensity; + textHeight = 20 * pixelDensity; + + needsInvertedColors = mapView.needsInvertedColors(); + boxX = metrics.widthPixels - boxWidth; + boxY = 0; + + maxTextWidth = boxWidth - 3 * boxPadding; + } + + public void setCoordinates(final Location coordinatesIn) { + currentCoords = new Geopoint(coordinatesIn); + + final float distance = currentCoords.distanceTo(destinationCoords); + distanceText = Units.getDistanceFromKilometers(distance); + } + + @Override + public void draw(final Canvas canvas, final MapViewImpl mapView, final boolean shadow) { + drawInternal(canvas); + } + + @Override + public void drawOverlayBitmap(final Canvas canvas, final Point drawPosition, final MapProjectionImpl projection, final byte drawZoomLevel) { + drawInternal(canvas); + } + + private void drawInternal(final Canvas canvas) { + if (currentCoords == null) { + return; + } + + if (blurBoxShadow == null) { + blurBoxShadow = new BlurMaskFilter(3, BlurMaskFilter.Blur.NORMAL); + } + + if (paintBoxShadow == null) { + paintBoxShadow = new Paint(); + paintBoxShadow.setAntiAlias(true); + paintBoxShadow.setMaskFilter(blurBoxShadow); + } + + if (paintBox == null) { + paintBox = new Paint(); + paintBox.setAntiAlias(true); + } + + if (paintText == null) { + paintText = new Paint(); + paintText.setAntiAlias(true); + paintText.setTextAlign(Paint.Align.LEFT); + paintText.setTypeface(Typeface.DEFAULT_BOLD); + } + + if (paintCompass == null) { + paintCompass = new Paint(Paint.ANTI_ALIAS_FLAG); + paintCompass.setDither(true); + paintCompass.setFilterBitmap(true); + } + + if (needsInvertedColors) { + paintBoxShadow.setColor(0xFF000000); + paintBox.setColor(0xFFFFFFFF); + paintText.setColor(0xFF000000); + } else { + paintBoxShadow.setColor(0xFFFFFFFF); + paintBox.setColor(0xFF000000); + paintText.setColor(0xFFFFFFFF); + } + + /* Calculate text size */ + final Rect textBounds = new Rect(); + paintText.setTextSize(textHeight); + paintText.getTextBounds(distanceText, 0, distanceText.length(), textBounds); + while (textBounds.height() > maxTextWidth) { + paintText.setTextSize(paintText.getTextSize() - 1); + paintText.getTextBounds(distanceText, 0, distanceText.length(), textBounds); + } + + final float textX = (boxWidth - 3 * boxPadding - textBounds.width()) / 2 + boxX + 2 * boxPadding; + final float textY = (boxHeight + textBounds.height()) / 2 + boxY; + + /* Paint background box */ + canvas.drawRoundRect( + new RectF( + boxX - boxShadowSize, boxY - boxShadowSize - boxCornerRadius, + boxX + boxWidth + boxShadowSize + boxCornerRadius, boxY + boxHeight + boxShadowSize + ), + boxCornerRadius, boxCornerRadius, + paintBoxShadow + ); + canvas.drawRoundRect( + new RectF( + boxX, boxY - boxCornerRadius, + boxX + boxWidth + boxCornerRadius, boxY + boxHeight + ), + boxCornerRadius, boxCornerRadius, + paintBox + ); + + /* Paint distance */ + canvas.drawText(distanceText, textX, textY, paintText); + } + + @Override + public OverlayImpl getOverlayImpl() { + return this.ovlImpl; + } + +} diff --git a/main/src/cgeo/geocaching/maps/google/v1/GoogleDistanceOverlay.java b/main/src/cgeo/geocaching/maps/google/v1/GoogleDistanceOverlay.java new file mode 100755 index 0000000..281a6e9 --- /dev/null +++ b/main/src/cgeo/geocaching/maps/google/v1/GoogleDistanceOverlay.java @@ -0,0 +1,53 @@ +package cgeo.geocaching.maps.google.v1; + +import cgeo.geocaching.location.Geopoint; +import cgeo.geocaching.maps.DistanceOverlay; +import cgeo.geocaching.maps.interfaces.GeneralOverlay; +import cgeo.geocaching.maps.interfaces.MapViewImpl; +import cgeo.geocaching.maps.interfaces.OverlayImpl; + +import com.google.android.maps.MapView; +import com.google.android.maps.Overlay; + +import android.graphics.Canvas; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +public class GoogleDistanceOverlay extends Overlay implements OverlayImpl { + + private final DistanceOverlay overlayBase; + private final Lock lock = new ReentrantLock(); + + public GoogleDistanceOverlay(final MapViewImpl mapView, final Geopoint coords, final String geocode) { + overlayBase = new DistanceOverlay(this, mapView, coords, geocode); + } + + @Override + public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) { + super.draw(canvas, mapView, shadow); + + assert mapView instanceof MapViewImpl; + overlayBase.draw(canvas, (MapViewImpl) mapView, shadow); + } + + public GeneralOverlay getBase() { + return overlayBase; + } + + @Override + public void lock() { + lock.lock(); + } + + @Override + public void unlock() { + lock.unlock(); + } + + @Override + public MapViewImpl getMapViewImpl() { + throw new UnsupportedOperationException(); + } + +} diff --git a/main/src/cgeo/geocaching/maps/google/v1/GoogleMapView.java b/main/src/cgeo/geocaching/maps/google/v1/GoogleMapView.java index d474e1d..08c9ae7 100644 --- a/main/src/cgeo/geocaching/maps/google/v1/GoogleMapView.java +++ b/main/src/cgeo/geocaching/maps/google/v1/GoogleMapView.java @@ -2,8 +2,10 @@ package cgeo.geocaching.maps.google.v1; import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; +import cgeo.geocaching.location.Geopoint; import cgeo.geocaching.location.Viewport; import cgeo.geocaching.maps.CachesOverlay; +import cgeo.geocaching.maps.DistanceOverlay; import cgeo.geocaching.maps.PositionAndScaleOverlay; import cgeo.geocaching.maps.interfaces.GeneralOverlay; import cgeo.geocaching.maps.interfaces.GeoPointImpl; @@ -36,22 +38,22 @@ public class GoogleMapView extends MapView implements MapViewImpl { private OnMapDragListener onDragListener; private final GoogleMapController mapController = new GoogleMapController(getController()); - public GoogleMapView(Context context, AttributeSet attrs) { + public GoogleMapView(final Context context, final AttributeSet attrs) { super(context, attrs); initialize(context); } - public GoogleMapView(Context context, AttributeSet attrs, int defStyle) { + public GoogleMapView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); initialize(context); } - public GoogleMapView(Context context, String apiKey) { + public GoogleMapView(final Context context, final String apiKey) { super(context, apiKey); initialize(context); } - private void initialize(Context context) { + private void initialize(final Context context) { if (isInEditMode()) { return; } @@ -66,16 +68,16 @@ public class GoogleMapView extends MapView implements MapViewImpl { } super.draw(canvas); - } catch (Exception e) { + } catch (final Exception e) { Log.e("GoogleMapView.draw", e); } } @Override - public void displayZoomControls(boolean takeFocus) { + public void displayZoomControls(final boolean takeFocus) { try { // Push zoom controls to the right - FrameLayout.LayoutParams zoomParams = new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT); + final FrameLayout.LayoutParams zoomParams = new FrameLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT); zoomParams.gravity = Gravity.RIGHT; // The call to retrieve the zoom buttons controller is undocumented and works so far on all devices // supported by Google Play, but fails at least on one Jolla. @@ -83,9 +85,9 @@ public class GoogleMapView extends MapView implements MapViewImpl { controller.getZoomControls().setLayoutParams(zoomParams); super.displayZoomControls(takeFocus); - } catch (NoSuchMethodException ignored) { + } catch (final NoSuchMethodException ignored) { Log.w("GoogleMapView.displayZoomControls: unable to explicitly place the zoom buttons"); - } catch (Exception e) { + } catch (final Exception e) { Log.e("GoogleMapView.displayZoomControls", e); } } @@ -98,7 +100,7 @@ public class GoogleMapView extends MapView implements MapViewImpl { @Override @NonNull public GeoPointImpl getMapViewCenter() { - GeoPoint point = getMapCenter(); + final GeoPoint point = getMapCenter(); return new GoogleGeoPoint(point.getLatitudeE6(), point.getLongitudeE6()); } @@ -118,9 +120,9 @@ public class GoogleMapView extends MapView implements MapViewImpl { } @Override - public CachesOverlay createAddMapOverlay(Context context, Drawable drawable) { + public CachesOverlay createAddMapOverlay(final Context context, final Drawable drawable) { - GoogleCacheOverlay ovl = new GoogleCacheOverlay(context, drawable); + final GoogleCacheOverlay ovl = new GoogleCacheOverlay(context, drawable); getOverlays().add(ovl); return ovl.getBase(); } @@ -128,12 +130,19 @@ public class GoogleMapView extends MapView implements MapViewImpl { @Override public PositionAndScaleOverlay createAddPositionAndScaleOverlay() { - GoogleOverlay ovl = new GoogleOverlay(); + final GoogleOverlay ovl = new GoogleOverlay(); getOverlays().add(ovl); return (PositionAndScaleOverlay) ovl.getBase(); } @Override + public DistanceOverlay createAddDistanceOverlay(final Geopoint coords, final String geocode) { + final GoogleDistanceOverlay ovl = new GoogleDistanceOverlay(this, coords, geocode); + getOverlays().add(ovl); + return (DistanceOverlay) ovl.getBase(); + } + + @Override public int getMapZoomLevel() { return getZoomLevel(); } @@ -144,21 +153,21 @@ public class GoogleMapView extends MapView implements MapViewImpl { } @Override - public void repaintRequired(GeneralOverlay overlay) { + public void repaintRequired(final GeneralOverlay overlay) { invalidate(); } @Override - public void setOnDragListener(OnMapDragListener onDragListener) { + public void setOnDragListener(final OnMapDragListener onDragListener) { this.onDragListener = onDragListener; } @Override - public boolean onTouchEvent(MotionEvent ev) { + public boolean onTouchEvent(final MotionEvent ev) { try { gestureDetector.onTouchEvent(ev); return super.onTouchEvent(ev); - } catch (Exception e) { + } catch (final Exception e) { Log.e("GoogleMapView.onTouchEvent", e); } return false; @@ -166,7 +175,7 @@ public class GoogleMapView extends MapView implements MapViewImpl { private class GestureListener extends SimpleOnGestureListener { @Override - public boolean onDoubleTap(MotionEvent e) { + public boolean onDoubleTap(final MotionEvent e) { getController().zoomInFixing((int) e.getX(), (int) e.getY()); if (onDragListener != null) { onDragListener.onDrag(); @@ -175,8 +184,8 @@ public class GoogleMapView extends MapView implements MapViewImpl { } @Override - public boolean onScroll(MotionEvent e1, MotionEvent e2, - float distanceX, float distanceY) { + public boolean onScroll(final MotionEvent e1, final MotionEvent e2, + final float distanceX, final float distanceY) { if (onDragListener != null) { onDragListener.onDrag(); } diff --git a/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java b/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java index 0560ad4..00a8790 100644 --- a/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java +++ b/main/src/cgeo/geocaching/maps/interfaces/MapViewImpl.java @@ -1,7 +1,9 @@ package cgeo.geocaching.maps.interfaces; +import cgeo.geocaching.location.Geopoint; import cgeo.geocaching.location.Viewport; import cgeo.geocaching.maps.CachesOverlay; +import cgeo.geocaching.maps.DistanceOverlay; import cgeo.geocaching.maps.PositionAndScaleOverlay; import org.eclipse.jdt.annotation.NonNull; @@ -48,6 +50,8 @@ public interface MapViewImpl { PositionAndScaleOverlay createAddPositionAndScaleOverlay(); + DistanceOverlay createAddDistanceOverlay(Geopoint coords, String geocode); + void setMapSource(); /** diff --git a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeDistanceOverlay.java b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeDistanceOverlay.java new file mode 100755 index 0000000..ed6b6fb --- /dev/null +++ b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeDistanceOverlay.java @@ -0,0 +1,56 @@ +package cgeo.geocaching.maps.mapsforge; + +import cgeo.geocaching.location.Geopoint; +import cgeo.geocaching.maps.DistanceOverlay; +import cgeo.geocaching.maps.interfaces.GeneralOverlay; +import cgeo.geocaching.maps.interfaces.MapViewImpl; +import cgeo.geocaching.maps.interfaces.OverlayImpl; + +import org.mapsforge.android.maps.Projection; +import org.mapsforge.android.maps.overlay.Overlay; + +import android.graphics.Canvas; +import android.graphics.Point; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +public class MapsforgeDistanceOverlay extends Overlay implements OverlayImpl { + + private DistanceOverlay overlayBase = null; + private final Lock lock = new ReentrantLock(); + + public MapsforgeDistanceOverlay(final MapViewImpl mapView, final Geopoint coords, final String geocode) { + overlayBase = new DistanceOverlay(this, mapView, coords, geocode); + } + + @Override + protected void drawOverlayBitmap(final Canvas canvas, final Point drawPosition, + final Projection projection, final byte drawZoomLevel) { + + if (overlayBase != null) { + overlayBase.drawOverlayBitmap(canvas, drawPosition, new MapsforgeMapProjection(projection), drawZoomLevel); + } + } + + public GeneralOverlay getBase() { + return overlayBase; + } + + @Override + public void lock() { + lock.lock(); + } + + @Override + public void unlock() { + lock.unlock(); + + } + + @Override + public MapViewImpl getMapViewImpl() { + return (MapViewImpl) internalMapView; + } + +} diff --git a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java index 73d87b0..dd67a8f 100644 --- a/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java +++ b/main/src/cgeo/geocaching/maps/mapsforge/MapsforgeMapView.java @@ -1,8 +1,10 @@ package cgeo.geocaching.maps.mapsforge; import cgeo.geocaching.R; +import cgeo.geocaching.location.Geopoint; import cgeo.geocaching.location.Viewport; import cgeo.geocaching.maps.CachesOverlay; +import cgeo.geocaching.maps.DistanceOverlay; import cgeo.geocaching.maps.PositionAndScaleOverlay; import cgeo.geocaching.maps.interfaces.GeneralOverlay; import cgeo.geocaching.maps.interfaces.GeoPointImpl; @@ -40,12 +42,12 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { private OnMapDragListener onDragListener; private final MapsforgeMapController mapController = new MapsforgeMapController(getController(), getMapGenerator().getZoomLevelMax()); - public MapsforgeMapView(Context context, AttributeSet attrs) { + public MapsforgeMapView(final Context context, final AttributeSet attrs) { super(context, attrs); initialize(context); } - private void initialize(Context context) { + private void initialize(final Context context) { if (isInEditMode()) { return; } @@ -56,7 +58,7 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { } @Override - public void draw(@NonNull Canvas canvas) { + public void draw(@NonNull final Canvas canvas) { try { // Google Maps and OSM Maps use different zoom levels for the same view. // Here we don't want the Google Maps compatible zoom level, but the actual one. @@ -65,13 +67,13 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { } super.draw(canvas); - } catch (Exception e) { + } catch (final Exception e) { Log.e("MapsforgeMapView.draw", e); } } @Override - public void displayZoomControls(boolean takeFocus) { + public void displayZoomControls(final boolean takeFocus) { // nothing to do here } @@ -83,7 +85,7 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { @Override @NonNull public GeoPointImpl getMapViewCenter() { - GeoPoint point = getMapPosition().getMapCenter(); + final GeoPoint point = getMapPosition().getMapCenter(); return new MapsforgeGeoPoint(point.latitudeE6, point.longitudeE6); } @@ -103,31 +105,38 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { } @Override - public CachesOverlay createAddMapOverlay(Context context, Drawable drawable) { + public CachesOverlay createAddMapOverlay(final Context context, final Drawable drawable) { - MapsforgeCacheOverlay ovl = new MapsforgeCacheOverlay(context, drawable); + final MapsforgeCacheOverlay ovl = new MapsforgeCacheOverlay(context, drawable); getOverlays().add(ovl); return ovl.getBase(); } @Override public PositionAndScaleOverlay createAddPositionAndScaleOverlay() { - MapsforgeOverlay ovl = new MapsforgeOverlay(); + final MapsforgeOverlay ovl = new MapsforgeOverlay(); getOverlays().add(ovl); return (PositionAndScaleOverlay) ovl.getBase(); } @Override + public DistanceOverlay createAddDistanceOverlay(final Geopoint coords, final String geocode) { + final MapsforgeDistanceOverlay ovl = new MapsforgeDistanceOverlay(this, coords, geocode); + getOverlays().add(ovl); + return (DistanceOverlay) ovl.getBase(); + } + + @Override public int getLatitudeSpan() { int span = 0; - Projection projection = getProjection(); + final Projection projection = getProjection(); if (projection != null && getHeight() > 0) { - GeoPoint low = projection.fromPixels(0, 0); - GeoPoint high = projection.fromPixels(0, getHeight()); + final GeoPoint low = projection.fromPixels(0, 0); + final GeoPoint high = projection.fromPixels(0, getHeight()); if (low != null && high != null) { span = Math.abs(high.latitudeE6 - low.latitudeE6); @@ -142,11 +151,11 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { int span = 0; - Projection projection = getProjection(); + final Projection projection = getProjection(); if (projection != null && getWidth() > 0) { - GeoPoint low = projection.fromPixels(0, 0); - GeoPoint high = projection.fromPixels(getWidth(), 0); + final GeoPoint low = projection.fromPixels(0, 0); + final GeoPoint high = projection.fromPixels(getWidth(), 0); if (low != null && high != null) { span = Math.abs(high.longitudeE6 - low.longitudeE6); @@ -191,7 +200,7 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { newMapType = ((MapsforgeMapSource) mapSource).getGenerator(); } - MapGenerator mapGenerator = MapGeneratorFactory.createMapGenerator(newMapType); + final MapGenerator mapGenerator = MapGeneratorFactory.createMapGenerator(newMapType); // When swapping map sources, make sure we aren't exceeding max zoom. See bug #1535 final int maxZoom = mapGenerator.getZoomLevelMax(); @@ -230,11 +239,11 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { @Override public void setMapTheme() { - String customRenderTheme = Settings.getCustomRenderThemeFilePath(); + final String customRenderTheme = Settings.getCustomRenderThemeFilePath(); if (StringUtils.isNotEmpty(customRenderTheme)) { try { setRenderTheme(new File(customRenderTheme)); - } catch (FileNotFoundException ignored) { + } catch (final FileNotFoundException ignored) { Toast.makeText( getContext(), getContext().getResources().getString(R.string.warn_rendertheme_missing), @@ -247,38 +256,38 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { } @Override - public void repaintRequired(GeneralOverlay overlay) { + public void repaintRequired(final GeneralOverlay overlay) { if (null == overlay) { invalidate(); } else { try { - Overlay ovl = (Overlay) overlay.getOverlayImpl(); + final Overlay ovl = (Overlay) overlay.getOverlayImpl(); if (ovl != null) { ovl.requestRedraw(); } - } catch (Exception e) { + } catch (final Exception e) { Log.e("MapsforgeMapView.repaintRequired", e); } } } @Override - public void setOnDragListener(OnMapDragListener onDragListener) { + public void setOnDragListener(final OnMapDragListener onDragListener) { this.onDragListener = onDragListener; } @Override - public boolean onTouchEvent(MotionEvent ev) { + public boolean onTouchEvent(final MotionEvent ev) { gestureDetector.onTouchEvent(ev); return super.onTouchEvent(ev); } private class GestureListener extends SimpleOnGestureListener { @Override - public boolean onDoubleTap(MotionEvent e) { + public boolean onDoubleTap(final MotionEvent e) { if (onDragListener != null) { onDragListener.onDrag(); } @@ -286,8 +295,8 @@ public class MapsforgeMapView extends MapView implements MapViewImpl { } @Override - public boolean onScroll(MotionEvent e1, MotionEvent e2, - float distanceX, float distanceY) { + public boolean onScroll(final MotionEvent e1, final MotionEvent e2, + final float distanceX, final float distanceY) { if (onDragListener != null) { onDragListener.onDrag(); } diff --git a/main/src/cgeo/geocaching/maps/mapsforge/v024/MapsforgeDistanceOverlay.java b/main/src/cgeo/geocaching/maps/mapsforge/v024/MapsforgeDistanceOverlay.java new file mode 100755 index 0000000..25b97f0 --- /dev/null +++ b/main/src/cgeo/geocaching/maps/mapsforge/v024/MapsforgeDistanceOverlay.java @@ -0,0 +1,57 @@ +package cgeo.geocaching.maps.mapsforge.v024; + +import cgeo.geocaching.location.Geopoint; +import cgeo.geocaching.maps.DistanceOverlay; +import cgeo.geocaching.maps.interfaces.GeneralOverlay; +import cgeo.geocaching.maps.interfaces.MapViewImpl; +import cgeo.geocaching.maps.interfaces.OverlayImpl; + +import org.mapsforge.android.mapsold.Overlay; +import org.mapsforge.android.mapsold.Projection; + +import android.graphics.Canvas; +import android.graphics.Point; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +public class MapsforgeDistanceOverlay extends Overlay implements OverlayImpl { + + private DistanceOverlay overlayBase = null; + private final Lock lock = new ReentrantLock(); + + public MapsforgeDistanceOverlay(final MapViewImpl mapView, final Geopoint coords, final String geocode) { + overlayBase = new DistanceOverlay(this, mapView, coords, geocode); + } + + @Override + protected void drawOverlayBitmap(final Canvas canvas, final Point drawPosition, + final Projection projection, final byte drawZoomLevel) { + + if (overlayBase != null) { + overlayBase.drawOverlayBitmap(canvas, drawPosition, new MapsforgeMapProjection(projection), drawZoomLevel); + } + } + + public GeneralOverlay getBase() { + return overlayBase; + } + + @Override + public void lock() { + lock.lock(); + + } + + @Override + public void unlock() { + lock.unlock(); + + } + + @Override + public MapViewImpl getMapViewImpl() { + return (MapViewImpl) internalMapView; + } + +} diff --git a/main/src/cgeo/geocaching/maps/mapsforge/v024/MapsforgeMapView024.java b/main/src/cgeo/geocaching/maps/mapsforge/v024/MapsforgeMapView024.java index 42c55fe..6b97707 100644 --- a/main/src/cgeo/geocaching/maps/mapsforge/v024/MapsforgeMapView024.java +++ b/main/src/cgeo/geocaching/maps/mapsforge/v024/MapsforgeMapView024.java @@ -1,8 +1,10 @@ package cgeo.geocaching.maps.mapsforge.v024; import cgeo.geocaching.R; +import cgeo.geocaching.location.Geopoint; import cgeo.geocaching.location.Viewport; import cgeo.geocaching.maps.CachesOverlay; +import cgeo.geocaching.maps.DistanceOverlay; import cgeo.geocaching.maps.PositionAndScaleOverlay; import cgeo.geocaching.maps.interfaces.GeneralOverlay; import cgeo.geocaching.maps.interfaces.GeoPointImpl; @@ -34,12 +36,12 @@ public class MapsforgeMapView024 extends MapView implements MapViewImpl { private OnMapDragListener onDragListener; private final MapsforgeMapController mapController = new MapsforgeMapController(getController(), getMaxZoomLevel()); - public MapsforgeMapView024(Context context, AttributeSet attrs) { + public MapsforgeMapView024(final Context context, final AttributeSet attrs) { super(context, attrs); initialize(context); } - private void initialize(Context context) { + private void initialize(final Context context) { if (isInEditMode()) { return; } @@ -47,7 +49,7 @@ public class MapsforgeMapView024 extends MapView implements MapViewImpl { } @Override - public void draw(@NonNull Canvas canvas) { + public void draw(@NonNull final Canvas canvas) { try { // Google Maps and OSM Maps use different zoom levels for the same view. // Here we don't want the Google Maps compatible zoom level, but the actual one. @@ -56,13 +58,13 @@ public class MapsforgeMapView024 extends MapView implements MapViewImpl { } super.draw(canvas); - } catch (Exception e) { + } catch (final Exception e) { Log.e("MapsforgeMapView024.draw", e); } } @Override - public void displayZoomControls(boolean takeFocus) { + public void displayZoomControls(final boolean takeFocus) { // nothing to do here } @@ -74,7 +76,7 @@ public class MapsforgeMapView024 extends MapView implements MapViewImpl { @Override @NonNull public GeoPointImpl getMapViewCenter() { - GeoPoint point = getMapCenter(); + final GeoPoint point = getMapCenter(); return new MapsforgeGeoPoint(point.getLatitudeE6(), point.getLongitudeE6()); } @@ -94,31 +96,38 @@ public class MapsforgeMapView024 extends MapView implements MapViewImpl { } @Override - public CachesOverlay createAddMapOverlay(Context context, Drawable drawable) { + public CachesOverlay createAddMapOverlay(final Context context, final Drawable drawable) { - MapsforgeCacheOverlay ovl = new MapsforgeCacheOverlay(context, drawable); + final MapsforgeCacheOverlay ovl = new MapsforgeCacheOverlay(context, drawable); getOverlays().add(ovl); return ovl.getBase(); } @Override public PositionAndScaleOverlay createAddPositionAndScaleOverlay() { - MapsforgeOverlay ovl = new MapsforgeOverlay(); + final MapsforgeOverlay ovl = new MapsforgeOverlay(); getOverlays().add(ovl); return (PositionAndScaleOverlay) ovl.getBase(); } @Override + public DistanceOverlay createAddDistanceOverlay(final Geopoint coords, final String geocode) { + final MapsforgeDistanceOverlay ovl = new MapsforgeDistanceOverlay(this, coords, geocode); + getOverlays().add(ovl); + return (DistanceOverlay) ovl.getBase(); + } + + @Override public int getLatitudeSpan() { int span = 0; - Projection projection = getProjection(); + final Projection projection = getProjection(); if (projection != null && getHeight() > 0) { - GeoPoint low = projection.fromPixels(0, 0); - GeoPoint high = projection.fromPixels(0, getHeight()); + final GeoPoint low = projection.fromPixels(0, 0); + final GeoPoint high = projection.fromPixels(0, getHeight()); if (low != null && high != null) { span = Math.abs(high.getLatitudeE6() - low.getLatitudeE6()); @@ -133,11 +142,11 @@ public class MapsforgeMapView024 extends MapView implements MapViewImpl { int span = 0; - Projection projection = getProjection(); + final Projection projection = getProjection(); if (projection != null && getWidth() > 0) { - GeoPoint low = projection.fromPixels(0, 0); - GeoPoint high = projection.fromPixels(getWidth(), 0); + final GeoPoint low = projection.fromPixels(0, 0); + final GeoPoint high = projection.fromPixels(getWidth(), 0); if (low != null && high != null) { span = Math.abs(high.getLongitudeE6() - low.getLongitudeE6()); @@ -188,38 +197,38 @@ public class MapsforgeMapView024 extends MapView implements MapViewImpl { } @Override - public void repaintRequired(GeneralOverlay overlay) { + public void repaintRequired(final GeneralOverlay overlay) { if (null == overlay) { invalidate(); } else { try { - Overlay ovl = (Overlay) overlay.getOverlayImpl(); + final Overlay ovl = (Overlay) overlay.getOverlayImpl(); if (ovl != null) { ovl.requestRedraw(); } - } catch (Exception e) { + } catch (final Exception e) { Log.e("MapsforgeMapView024.repaintRequired", e); } } } @Override - public void setOnDragListener(OnMapDragListener onDragListener) { + public void setOnDragListener(final OnMapDragListener onDragListener) { this.onDragListener = onDragListener; } @Override - public boolean onTouchEvent(MotionEvent ev) { + public boolean onTouchEvent(final MotionEvent ev) { gestureDetector.onTouchEvent(ev); return super.onTouchEvent(ev); } private class GestureListener extends SimpleOnGestureListener { @Override - public boolean onDoubleTap(MotionEvent e) { + public boolean onDoubleTap(final MotionEvent e) { if (onDragListener != null) { onDragListener.onDrag(); } @@ -227,8 +236,8 @@ public class MapsforgeMapView024 extends MapView implements MapViewImpl { } @Override - public boolean onScroll(MotionEvent e1, MotionEvent e2, - float distanceX, float distanceY) { + public boolean onScroll(final MotionEvent e1, final MotionEvent e2, + final float distanceX, final float distanceY) { if (onDragListener != null) { onDragListener.onDrag(); } |
