Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix my location icon with enabled Snap to Road #19393

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions OsmAnd/src/net/osmand/plus/profiles/LocationIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@
import net.osmand.plus.R;

public enum LocationIcon {

DEFAULT(R.drawable.map_location_default, R.drawable.map_location_default_view_angle),
CAR(R.drawable.map_location_car, R.drawable.map_location_car_view_angle),
BICYCLE(R.drawable.map_location_bicycle, R.drawable.map_location_bicycle_view_angle);

@DrawableRes
private final int iconId;
@DrawableRes
private final int headingIconId;

LocationIcon(@DrawableRes int iconId, @DrawableRes int headingIconId) {
this.iconId = iconId;
this.headingIconId = headingIconId;
}

@DrawableRes
private final int iconId;
@DrawableRes
private final int headingIconId;

public int getIconId() {
return iconId;
}

@DrawableRes
public int getHeadingIconId() {
return headingIconId;
}
Expand Down
1 change: 1 addition & 0 deletions OsmAnd/src/net/osmand/plus/profiles/NavigationIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum NavigationIcon {
@DrawableRes
private final int iconId;

@DrawableRes
public int getIconId() {
return iconId;
}
Expand Down
9 changes: 4 additions & 5 deletions OsmAnd/src/net/osmand/plus/routing/RoutingHelperUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ public static void approximateBearingIfNeeded(@NonNull RoutingHelper routingHelp
@NonNull Location previousRouteLocation,
@NonNull Location currentRouteLocation,
@NonNull Location nextRouteLocation) {
double dist = location.distanceTo(projection);
double maxDist = routingHelper.getMaxAllowedProjectDist(currentRouteLocation);
if (location.distanceTo(projection) >= maxDist) {
if (dist >= maxDist) {
return;
}

Expand All @@ -131,12 +132,10 @@ public static void approximateBearingIfNeeded(@NonNull RoutingHelper routingHelp
* projectionOffsetN;
float approximatedBearing = MapUtils.normalizeDegrees360(currentSegmentBearing + segmentsBearingDelta);

boolean setApproximated;
if (location.hasBearing()) {
boolean setApproximated = true;
if (location.hasBearing() && dist >= maxDist / 2) {
float rotationDiff = MapUtils.unifyRotationDiff(location.getBearing(), approximatedBearing);
setApproximated = Math.abs(rotationDiff) < MAX_BEARING_DEVIATION;
} else {
setApproximated = true;
}
if (setApproximated) {
projection.setBearing(approximatedBearing);
Expand Down
73 changes: 49 additions & 24 deletions OsmAnd/src/net/osmand/plus/views/layers/PointLocationLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.plus.views.layers.ContextMenuLayer.IContextMenuProvider;
import net.osmand.plus.views.layers.base.OsmandMapLayer;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;

import java.util.List;
Expand Down Expand Up @@ -223,14 +224,8 @@ public boolean areMapRendererViewEventsAllowed() {
public void onUpdateFrame(MapRendererView mapRenderer) {
super.onUpdateFrame(mapRenderer);
if (isMapLinkedToLocation() && !isMovingToMyLocation()) {
OsmandApplication app = getApplication();
Location lastKnownLocation = locationProvider.getLastStaleKnownLocation();
Boolean snapToRoad = app.getSettings().SNAP_TO_ROAD.get();
boolean followingMode = app.getRoutingHelper().isFollowingMode();
Location lastRouteProjection = followingMode && snapToRoad
? app.getOsmandMap().getMapLayers().getRouteLayer().getLastRouteProjection() : null;
Location location = getPointLocation();
PointI target31 = mapRenderer.getTarget();
Location location = lastRouteProjection != null ? lastRouteProjection : lastKnownLocation;
updateMarker(location, target31, 0);
}
lastMarkerLocation = getCurrentMarkerLocation();
Expand Down Expand Up @@ -365,6 +360,7 @@ private void updateMarkerPosition(@NonNull Location location, @Nullable PointI t
locMarker.marker.setPosition(target31);
}
locMarker.marker.setAccuracyCircleRadius(location.getAccuracy());
locMarker.marker.setIsAccuracyCircleVisible(!isLocationSnappedToRoad());
}
}

Expand Down Expand Up @@ -422,7 +418,7 @@ private LatLon getCurrentMarkerLocation() {
}

private boolean shouldShowHeading() {
return !locationOutdated && mapViewTrackingUtilities.isShowViewAngle();
return !locationOutdated && mapViewTrackingUtilities.isShowViewAngle() && !isLocationSnappedToRoad();
}

private boolean shouldShowBearing(@Nullable Location location) {
Expand All @@ -434,8 +430,10 @@ private Float getBearingToShow(@Nullable Location location) {
if (!locationOutdated && location != null) {
// Issue 5538: Some devices return positives for hasBearing() at rest, hence add 0.0 check:
boolean hasBearing = location.hasBearing() && location.getBearing() != 0.0f;
if ((hasBearing || isUseRouting() && lastBearingCached != null)
&& (!location.hasSpeed() || location.getSpeed() > BEARING_SPEED_THRESHOLD)) {
boolean bearingValid = hasBearing || isUseRouting() && lastBearingCached != null;
boolean speedValid = !location.hasSpeed() || location.getSpeed() > BEARING_SPEED_THRESHOLD;

if (bearingValid && (speedValid || isLocationSnappedToRoad())) {
return hasBearing ? location.getBearing() : lastBearingCached;
}
}
Expand All @@ -448,6 +446,22 @@ private boolean isUseRouting() {
|| routingHelper.isRouteBeingCalculated() || routingHelper.isRouteCalculated();
}

private boolean isLocationSnappedToRoad() {
OsmandApplication app = getApplication();
Location projection = app.getOsmandMap().getMapLayers().getRouteLayer().getLastRouteProjection();
return app.getSettings().SNAP_TO_ROAD.get() && Algorithms.objectEquals(projection, getPointLocation());
}

@Nullable
public Location getPointLocation() {
Location location = null;
OsmandApplication app = getApplication();
if (app.getRoutingHelper().isFollowingMode() && app.getSettings().SNAP_TO_ROAD.get()) {
location = app.getOsmandMap().getMapLayers().getRouteLayer().getLastRouteProjection();
}
return location != null ? location : locationProvider.getLastStaleKnownLocation();
}

private boolean isLocationVisible(@NonNull RotatedTileBox tb, @NonNull Location l) {
return tb.containsLatLon(l.getLatitude(), l.getLongitude());
}
Expand All @@ -464,23 +478,13 @@ private void drawMarkers(@NonNull Canvas canvas, @NonNull RotatedTileBox box, @N
locationX = box.getPixXFromLonNoRot(lastKnownLocation.getLongitude());
locationY = box.getPixYFromLatNoRot(lastKnownLocation.getLatitude());
}

double dist = box.getDistance(0, box.getPixHeight() / 2, box.getPixWidth(), box.getPixHeight() / 2);
int radius = (int) (((double) box.getPixWidth()) / dist * lastKnownLocation.getAccuracy());
if (radius > RADIUS * box.getDensity()) {
int allowedRad = Math.min(box.getPixWidth() / 2, box.getPixHeight() / 2);
canvas.drawCircle(locationX, locationY, Math.min(radius, allowedRad), area);
canvas.drawCircle(locationX, locationY, Math.min(radius, allowedRad), aroundArea);
if (!isLocationSnappedToRoad()) {
drawLocationAccuracy(canvas, box, lastKnownLocation, locationX, locationY);
}
// draw bearing/direction/location
if (isLocationVisible(box, lastKnownLocation)) {
Float heading = locationProvider.getHeading();
if (shouldShowHeading() && heading != null) {
canvas.save();
canvas.rotate(heading - 180, locationX, locationY);
canvas.drawBitmap(headingIcon, locationX - headingIcon.getWidth() / 2f,
locationY - headingIcon.getHeight() / 2f, headingPaint);
canvas.restore();
if (shouldShowHeading()) {
drawLocationHeading(canvas, locationX, locationY);
}
Float bearing = getBearingToShow(lastKnownLocation);
if (bearing != null) {
Expand All @@ -492,6 +496,27 @@ private void drawMarkers(@NonNull Canvas canvas, @NonNull RotatedTileBox box, @N
}
}

private void drawLocationAccuracy(@NonNull Canvas canvas, @NonNull RotatedTileBox box,
@NonNull Location location, int locationX, int locationY) {
double dist = box.getDistance(0, box.getPixHeight() / 2, box.getPixWidth(), box.getPixHeight() / 2);
int radius = (int) (((double) box.getPixWidth()) / dist * location.getAccuracy());
if (radius > RADIUS * box.getDensity()) {
int allowedRad = Math.min(box.getPixWidth() / 2, box.getPixHeight() / 2);
canvas.drawCircle(locationX, locationY, Math.min(radius, allowedRad), area);
canvas.drawCircle(locationX, locationY, Math.min(radius, allowedRad), aroundArea);
}
}

private void drawLocationHeading(@NonNull Canvas canvas, int locationX, int locationY) {
Float heading = locationProvider.getHeading();
if (heading != null) {
canvas.save();
canvas.rotate(heading - 180, locationX, locationY);
canvas.drawBitmap(headingIcon, locationX - headingIcon.getWidth() / 2f,
locationY - headingIcon.getHeight() / 2f, headingPaint);
canvas.restore();
}
}

@Override
public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) {
Expand Down