From e141400eb4aa3dceb971ac43e48d202ad2cf0ba7 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Thu, 21 Sep 2017 16:47:26 +0300 Subject: [PATCH] Draw distance between marker and location; reset path in calculatePath() --- .../osmand/plus/views/MapMarkersLayer.java | 38 ++++++++++++++++++- .../net/osmand/plus/views/OsmandMapLayer.java | 15 ++++---- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java b/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java index 556e90c20f..092fdeec14 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java @@ -5,9 +5,11 @@ import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; +import android.graphics.PathMeasure; import android.graphics.PointF; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; +import android.graphics.Rect; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; @@ -19,6 +21,7 @@ import net.osmand.data.QuadPoint; import net.osmand.data.RotatedTileBox; import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper.MapMarker; +import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.TargetPointsHelper.TargetPoint; @@ -27,6 +30,7 @@ import net.osmand.plus.views.ContextMenuLayer.ApplyMovedObjectCallback; import net.osmand.plus.views.ContextMenuLayer.IContextMenuProvider; import net.osmand.plus.views.ContextMenuLayer.IContextMenuProviderSelection; import net.osmand.plus.views.mapwidgets.MapMarkersWidgetsFactory; +import net.osmand.util.MapUtils; import java.util.ArrayList; import java.util.List; @@ -37,6 +41,8 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi IContextMenuProviderSelection, ContextMenuLayer.IMoveObjectProvider { protected static final int DIST_TO_SHOW = 80; + private static final int TEXT_SIZE = 14; + private static final int VERTICAL_OFFSET = 10; private final MapActivity map; private OsmandMapTileView view; @@ -63,6 +69,7 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi private float[] calculations = new float[2]; private final RenderingLineAttributes lineAttrs = new RenderingLineAttributes("measureDistanceLine"); + private final RenderingLineAttributes textAttrs = new RenderingLineAttributes("rulerCircle"); private Paint paint; private Path path; private List route = new ArrayList<>(); @@ -113,6 +120,10 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi paint.setColor(ContextCompat.getColor(map, R.color.marker_red)); paint.setAlpha(200); + float textSize = TEXT_SIZE * map.getResources().getDisplayMetrics().density; + textAttrs.paint2.setTextSize(textSize); + textAttrs.paint3.setTextSize(textSize); + widgetsFactory = new MapMarkersWidgetsFactory(map); contextMenuLayer = view.getLayerByClass(ContextMenuLayer.class); @@ -199,8 +210,6 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi return; } - lineAttrs.updatePaints(view, nightMode, tileBox); - MapMarkersHelper markersHelper = map.getMyApplication().getMapMarkersHelper(); if (route.size() > 0) { path.reset(); @@ -228,6 +237,10 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi List activeMapMarkers = markersHelper.getMapMarkers(); if (settings.SHOW_LINES_TO_FIRST_MARKERS.get() && myLoc != null) { + lineAttrs.updatePaints(view, nightMode, tileBox); + textAttrs.updatePaints(view, nightMode, tileBox); + textAttrs.paint2.setStyle(Paint.Style.FILL); + int locX = tileBox.getPixXFromLonNoRot(myLoc.getLongitude()); int locY = tileBox.getPixYFromLatNoRot(myLoc.getLatitude()); int[] colors = MapMarker.getColors(map); @@ -235,6 +248,7 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi MapMarker marker = activeMapMarkers.get(i); int markerX = tileBox.getPixXFromLonNoRot(marker.getLongitude()); int markerY = tileBox.getPixYFromLatNoRot(marker.getLatitude()); + linePath.reset(); tx.clear(); ty.clear(); @@ -244,9 +258,29 @@ public class MapMarkersLayer extends OsmandMapLayer implements IContextMenuProvi ty.add(locY); tx.add(markerX); ty.add(markerY); + calculatePath(tileBox, tx, ty, linePath); + PathMeasure pm = new PathMeasure(linePath, false); + + float dist = (float) MapUtils.getDistance(myLoc.getLatitude(), myLoc.getLongitude(), marker.getLatitude(), marker.getLongitude()); + String text = OsmAndFormatter.getFormattedDistance(dist, view.getApplication()); + Rect bounds = new Rect(); + textAttrs.paint2.getTextBounds(text, 0, text.length(), bounds); + float hOffset = pm.getLength() / 2 - bounds.width() / 2; + lineAttrs.paint.setColor(colors[marker.colorIndex]); canvas.drawPath(linePath, lineAttrs.paint); + if (locX - markerX >= 0) { + float[] pos = new float[2]; + pm.getPosTan(pm.getLength() / 2, pos, null); + canvas.rotate(180, pos[0], pos[1]); + canvas.drawTextOnPath(text, linePath, hOffset, bounds.height() + VERTICAL_OFFSET, textAttrs.paint3); + canvas.drawTextOnPath(text, linePath, hOffset, bounds.height() + VERTICAL_OFFSET, textAttrs.paint2); + canvas.rotate(-180, pos[0], pos[1]); + } else { + canvas.drawTextOnPath(text, linePath, hOffset, -VERTICAL_OFFSET, textAttrs.paint3); + canvas.drawTextOnPath(text, linePath, hOffset, -VERTICAL_OFFSET, textAttrs.paint2); + } } } diff --git a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java index 0c2592cd07..00eeb91d50 100644 --- a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java @@ -138,9 +138,9 @@ public abstract class OsmandMapLayer { return x >= lx && x <= rx && y >= ty && y <= by; } - public int calculatePath(RotatedTileBox tb, TIntArrayList xs, TIntArrayList ys, Path path) { + path.reset(); boolean start = false; int px = xs.get(0); int py = ys.get(0); @@ -156,8 +156,7 @@ public abstract class OsmandMapLayer { if (pin && in) { draw = true; } else { - long intersection = MapAlgorithms.calculateIntersection(x, y, - px, py, 0, w, h, 0); + long intersection = MapAlgorithms.calculateIntersection(x, y, px, py, 0, w, h, 0); if (intersection != -1) { if (pin && (i == 1)) { cnt++; @@ -335,7 +334,7 @@ public abstract class OsmandMapLayer { public boolean isPaint_1; public int defaultWidth_1 = 0; private String renderingAttribute; - + public RenderingLineAttributes(String renderingAttribute) { this.renderingAttribute = renderingAttribute; paint = initPaint(); @@ -344,8 +343,8 @@ public abstract class OsmandMapLayer { paint_1 = initPaint(); shadowPaint = initPaint(); } - - + + private Paint initPaint() { Paint paint = new Paint(); paint.setStyle(Style.STROKE); @@ -411,11 +410,11 @@ public abstract class OsmandMapLayer { paint.setColor(defaultColor); } } - + private int calculateHash(Object... o) { return Arrays.hashCode(o); } - + public void drawPath(Canvas canvas, Path path) { if (isPaint_1) { canvas.drawPath(path, paint_1);