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

Auto zoom draft #18413

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
8 changes: 6 additions & 2 deletions OsmAnd-java/src/main/java/net/osmand/util/MapUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -758,11 +758,15 @@ public static boolean areLatLonEqualPrecise(Location l, double lat, double lon)
}

public static LatLon rhumbDestinationPoint(LatLon latLon, double distance, double bearing) {
return rhumbDestinationPoint(latLon.getLatitude(), latLon.getLongitude(), distance, bearing);
}

public static LatLon rhumbDestinationPoint(double lat, double lon, double distance, double bearing) {
double radius = EARTH_RADIUS_A;

double d = distance / radius; // angular distance in radians
double phi1 = Math.toRadians(latLon.getLatitude());
double lambda1 = Math.toRadians(latLon.getLongitude());
double phi1 = Math.toRadians(lat);
double lambda1 = Math.toRadians(lon);
double theta = Math.toRadians(bearing);

double deltaPhi = d * Math.cos(theta);
Expand Down
2 changes: 2 additions & 0 deletions OsmAnd/src/net/osmand/plus/AppInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.osmand.plus.backup.BackupHelper;
import net.osmand.plus.backup.NetworkSettingsHelper;
import net.osmand.plus.base.MapViewTrackingUtilities;
import net.osmand.plus.base.MyLocationAnimation;
import net.osmand.plus.base.dialog.DialogManager;
import net.osmand.plus.download.LocalIndexHelper;
import net.osmand.plus.download.LocalIndexInfo;
Expand Down Expand Up @@ -350,6 +351,7 @@ public void onCreateApplication() {
app.searchUICore = startupInit(new QuickSearchHelper(app), QuickSearchHelper.class);
app.mapViewTrackingUtilities = startupInit(new MapViewTrackingUtilities(app), MapViewTrackingUtilities.class);
app.osmandMap = startupInit(new OsmandMap(app), OsmandMap.class);
app.myLocationAnimation = startupInit(new MyLocationAnimation(app), MyLocationAnimation.class);

app.travelHelper = startupInit(new TravelObfHelper(app), TravelHelper.class);
app.travelRendererHelper = startupInit(new TravelRendererHelper(app), TravelRendererHelper.class);
Expand Down
2 changes: 1 addition & 1 deletion OsmAnd/src/net/osmand/plus/OsmAndLocationSimulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public void run() {
meters = (float) result.get(1);
}
float speed = meters / intervalTime * coeff;
if (intervalTime != 0 && speedSimulation) {
if (intervalTime != 0) {
current.setSpeed(speed);
}
if ((!current.hasAccuracy() || Double.isNaN(current.getAccuracy()) ||
Expand Down
6 changes: 6 additions & 0 deletions OsmAnd/src/net/osmand/plus/OsmandApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import net.osmand.plus.backup.BackupHelper;
import net.osmand.plus.backup.NetworkSettingsHelper;
import net.osmand.plus.base.MapViewTrackingUtilities;
import net.osmand.plus.base.MyLocationAnimation;
import net.osmand.plus.base.dialog.DialogManager;
import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.download.DownloadService;
Expand Down Expand Up @@ -191,6 +192,7 @@ public class OsmandApplication extends MultiDexApplication {
InAppPurchaseHelper inAppPurchaseHelper;
MapViewTrackingUtilities mapViewTrackingUtilities;
OsmandMap osmandMap;
MyLocationAnimation myLocationAnimation;
LockHelper lockHelper;
KeyEventHelper keyEventHelper;
FileSettingsHelper fileSettingsHelper;
Expand Down Expand Up @@ -1091,6 +1093,10 @@ public OsmandMap getOsmandMap() {
return osmandMap;
}

public MyLocationAnimation getMyLocationAnimation() {
return myLocationAnimation;
}

public boolean useOpenGlRenderer() {
return NativeCoreContext.isInit() && settings.USE_OPENGL_RENDER.get();
}
Expand Down
132 changes: 78 additions & 54 deletions OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,65 +220,16 @@ public void updateLocation(Location location) {
if (mapView != null) {
RotatedTileBox tb = mapView.getCurrentRotatedTileBox().copy();
if (isMapLinkedToLocation() && location != null) {
Pair<Integer, Double> zoom = null;
Float rotation = null;
boolean pendingRotation = false;
if (settings.AUTO_ZOOM_MAP.get()) {
zoom = autozoom(tb, location);
}
int currentMapRotation = settings.ROTATE_MAP.get();
boolean smallSpeedForCompass = isSmallSpeedForCompass(location);
boolean smallSpeedForAnimation = isSmallSpeedForAnimation(location);

showViewAngle = (!location.hasBearing() || smallSpeedForCompass) && (tb != null &&
NativeUtilities.containsLatLon(mapView.getMapRenderer(), tb, location.getLatitude(), location.getLongitude()));
if (currentMapRotation == OsmandSettings.ROTATE_MAP_BEARING) {
// special case when bearing equals to zero (we don't change anything)
if (location.hasBearing() && location.getBearing() != 0f) {
rotation = -location.getBearing();
}
if (rotation == null && prevLocation != null && tb != null) {
double distDp = (tb.getPixDensity() * MapUtils.getDistance(prevLocation, location)) / tb.getDensity();
if (distDp > SKIP_ANIMATION_DP_THRESHOLD) {
movingTime = 0;
}
}
} else if (currentMapRotation == OsmandSettings.ROTATE_MAP_COMPASS) {
if (currentMapRotation == OsmandSettings.ROTATE_MAP_COMPASS) {
showViewAngle = routePlanningMode; // disable compass rotation in that mode
pendingRotation = true;
} else if (currentMapRotation == OsmandSettings.ROTATE_MAP_NONE) {
rotation = 0.0f;
pendingRotation = true;
} else if (currentMapRotation == OsmandSettings.ROTATE_MAP_MANUAL) {
pendingRotation = true;
}
registerUnregisterSensor(location, smallSpeedForCompass);
if (settings.ANIMATE_MY_LOCATION.get() && !smallSpeedForAnimation && !movingToMyLocation) {
mapView.getAnimatedDraggingThread().startMoving(
location.getLatitude(), location.getLongitude(), zoom,
pendingRotation, rotation, movingTime, false,
() -> movingToMyLocation = false);
} else {
if (mapView.hasMapRenderer()) {
movingTime = movingToMyLocation
? (long) Math.min(movingTime * 0.7, MOVE_ANIMATION_TIME) : MOVE_ANIMATION_TIME;
if (mapView.getSettings().DO_NOT_USE_ANIMATIONS.get()) {
movingTime = 0;
}
mapView.getAnimatedDraggingThread().startMoving(
location.getLatitude(), location.getLongitude(), zoom,
pendingRotation, rotation, movingTime, false,
() -> movingToMyLocation = false);
} else {
if (zoom != null && zoom.first != null && zoom.second != null) {
mapView.getAnimatedDraggingThread().startZooming(zoom.first, zoom.second, null, false);
}
if (rotation != null) {
mapView.setRotate(rotation, false);
}
mapView.setLatLon(location.getLatitude(), location.getLongitude());
}
showViewAngle = (!location.hasBearing() || smallSpeedForCompass)
&& NativeUtilities.containsLatLon(mapView.getMapRenderer(), tb, location.getLatitude(), location.getLongitude());
}
registerUnregisterSensor(location, smallSpeedForCompass);
} else if (location != null) {
showViewAngle = (!location.hasBearing() || isSmallSpeedForCompass(location)) && (tb != null &&
NativeUtilities.containsLatLon(mapView.getMapRenderer(), tb, location.getLatitude(), location.getLongitude()));
Expand Down Expand Up @@ -383,7 +334,7 @@ public Pair<Integer, Double> autozoom(RotatedTileBox tb, Location location) {
// decrease a bit
zdelta += 1;
}
double targetZoom = Math.min(tb.getZoom() + tb.getZoomFloatPart() + zdelta, settings.AUTO_ZOOM_MAP_SCALE.get().maxZoom);
double targetZoom = Math.min(tb.getZoom() + tb.getZoomFloatPart() + zdelta, settings.AUTO_ZOOM_MAP_SCALE.get().maxZoomFromSpeed);
boolean isUserZoomed = lastTimeManualZooming > lastTimeAutoZooming;
int threshold = settings.AUTO_FOLLOW_ROUTE.get();
if ((now - lastTimeAutoZooming > AUTO_ZOOM_DEFAULT_CHANGE_ZOOM && !isUserZoomed)
Expand All @@ -403,6 +354,75 @@ public Pair<Integer, Double> autozoom(RotatedTileBox tb, Location location) {
return null;
}

// @Nullable
// private Pair<Integer, Double> calculateAutoZoom(@NonNull MapRendererView mapRenderer, @NonNull RotatedTileBox tileBox, @NonNull Location location) {
// if (!shouldAutoZoom(location)) {
// return null;
// }
//
// LatLon target = tileBox.getLatLonFromPixel(tileBox.getCenterPixelX(), tileBox.getPixHeight() / 3f);
//
// NextDirectionInfo nextDirectionInfo = new NextDirectionInfo();
// app.getRoutingHelper().getNextRouteDirectionInfo(nextDirectionInfo, true);
// if (nextDirectionInfo.distanceTo > 0 && nextDirectionInfo.directionInfo != null) {
// Location turnLocation = app.getRoutingHelper().getLocationFromRouteDirection(nextDirectionInfo.directionInfo);
//
// if (turnLocation != null) {
// PointI point31 = NativeUtilities.getPoint31FromLatLon(turnLocation.getLatitude(), turnLocation.getLongitude());
// if (mapRenderer.isPositionVisible(point31)) {
// double distanceToTurnLocation = MapUtils.getDistance(turnLocation.getLatitude(), turnLocation.getLongitude(), location.getLatitude(), location.getLongitude());
// double distanceToTarget = MapUtils.getDistance(target, location.getLatitude(), location.getLongitude());
// if (distanceToTurnLocation <= distanceToTarget) {
//
// }
// }
// }
// }
//
// AutoZoomMap autoZoomScale = settings.AUTO_ZOOM_MAP_SCALE.get();
//
// int currentIntZoom = tileBox.getZoom();
// float currentZoomFloatPart = (float) tileBox.getZoomFloatPart();
//
// RotatedTileBox intZoomTileBox = tileBox.copy();
// intZoomTileBox.setZoomAndAnimation(currentIntZoom, 0, 0);
//
//
// float currentMetersToTarget = (float) MapUtils.getDistance(focusLocation, location.getLatitude(), location.getLongitude());
// float requiredMetersToTarget = location.getSpeed() * 40 / autoZoomScale.coefficient;
// float zoomDelta = (float) (Math.log(currentMetersToTarget / requiredMetersToTarget) / Math.log(2)) - currentZoomFloatPart;
//
// int minZoom = mapView.getMinZoom();
// int maxZoom = Math.min(mapView.getMaxZoom(), Math.round(autoZoomScale.maxZoom)); // todo float zoom
// Zoom zoom = new Zoom(currentIntZoom, currentZoomFloatPart, minZoom, maxZoom);
// zoom.calculateAnimatedZoom(currentIntZoom, zoomDelta);
//
// if (Math.abs(zoomDelta) < 0.5f) {
// return null;
// }
//
// Log.v("M_MapViewTrackingUtilities", "--------------");
// Log.v("M_MapViewTrackingUtilities", "Current dist: " + currentMetersToTarget);
// Log.v("M_MapViewTrackingUtilities", "Expected dist: " + requiredMetersToTarget);
// Log.v("M_MapViewTrackingUtilities", "Current zoom: " + currentIntZoom + currentZoomFloatPart);
// Log.v("M_MapViewTrackingUtilities", "Expected zoom: " + zoom.getZoom());
// Log.v("M_MapViewTrackingUtilities", "Zoom delta: " + (zoom.getZoom() - currentIntZoom - currentZoomFloatPart));
// return new Pair<>(zoom.getBaseZoom(), (double) (zoom.getZoomFloatPart() + zoom.getZoomAnimation()));
// }

private boolean shouldAutoZoom(@NonNull Location location) {
if (!location.hasSpeed() || location.getSpeed() < 7 / 3.6f) {
return false;
}

boolean isUserZoomed = lastTimeManualZooming > lastTimeAutoZooming;
long now = System.currentTimeMillis();
int autoZoomAfterUserThreshold = Math.max(settings.AUTO_FOLLOW_ROUTE.get(), AUTO_ZOOM_DEFAULT_CHANGE_ZOOM);
boolean zoomAfterUserZoom = isUserZoomed && now - lastTimeManualZooming > autoZoomAfterUserThreshold;
boolean zoomAfterAutoZoom = !isUserZoomed && now - lastTimeAutoZooming > AUTO_ZOOM_DEFAULT_CHANGE_ZOOM;
return zoomAfterUserZoom || zoomAfterAutoZoom;
}

public void backToLocationImpl() {
backToLocationImpl(15, true);
}
Expand Down Expand Up @@ -492,6 +512,10 @@ public void setMapLinkedToLocation(boolean isMapLinkedToLocation) {
}
}

public void setIsMovingToMyLocation(boolean movingToMyLocation) {
this.movingToMyLocation = movingToMyLocation;
}

public boolean isMovingToMyLocation() {
return movingToMyLocation;
}
Expand Down
Loading