Fix map centering after route calculation

This commit is contained in:
Alexey Kulish 2016-02-23 10:50:01 +03:00
parent 143ce6922f
commit 4993afa967
3 changed files with 85 additions and 40 deletions

View file

@ -72,11 +72,14 @@ import net.osmand.plus.dialogs.ErrorBottomSheetDialog;
import net.osmand.plus.dialogs.RateUsBottomSheetDialog;
import net.osmand.plus.dialogs.WhatsNewDialogFragment;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.GpxImportHelper;
import net.osmand.plus.helpers.WakeLockHelper;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.MapContextMenuFragment;
import net.osmand.plus.mapcontextmenu.other.DestinationReachedMenu;
import net.osmand.plus.mapcontextmenu.other.MapRouteInfoMenu;
import net.osmand.plus.mapcontextmenu.other.MapRouteInfoMenuFragment;
import net.osmand.plus.render.RendererRegistry;
import net.osmand.plus.resources.ResourceManager;
import net.osmand.plus.routing.RoutingHelper;
@ -139,7 +142,7 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents,
private OsmandApplication app;
private OsmandSettings settings;
boolean firstTime;
private boolean landscapeLayout;
private Dialog progressDlg = null;
@ -182,6 +185,10 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents,
app.applyTheme(this);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
boolean portraitMode = AndroidUiHelper.isOrientationPortrait(this);
boolean largeDevice = AndroidUiHelper.isXLargeDevice(this);
landscapeLayout = !portraitMode && !largeDevice;
mapContextMenu.setMapActivity(this);
super.onCreate(savedInstanceState);
@ -1181,6 +1188,63 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents,
@Override
public void newRouteIsCalculated(boolean newRoute, ValueHolder<Boolean> showToast) {
RoutingHelper rh = app.getRoutingHelper();
if (newRoute && rh.isRoutePlanningMode() && mapView != null) {
RotatedTileBox rt = mapView.getCurrentRotatedTileBox();
Location lt = rh.getLastProjection();
if (lt == null) {
lt = app.getTargetPointsHelper().getPointToStartLocation();
}
if (lt != null) {
double left = lt.getLongitude(), right = lt.getLongitude();
double top = lt.getLatitude(), bottom = lt.getLatitude();
List<TargetPoint> list = app.getTargetPointsHelper().getIntermediatePointsWithTarget();
for (TargetPoint l : list) {
left = Math.min(left, l.getLongitude());
right = Math.max(right, l.getLongitude());
top = Math.max(top, l.getLatitude());
bottom = Math.min(bottom, l.getLatitude());
}
RotatedTileBox tb = new RotatedTileBox(rt);
double border = 0.8;
int dy = 0;
MapRouteInfoMenu routeInfoMenu = mapLayers.getMapControlsLayer().getMapRouteInfoMenu();
boolean routeMenuVisible = false;
int tbw = (int) (tb.getPixWidth() * border);
int tbh = (int) (tb.getPixHeight() * border);
WeakReference<MapRouteInfoMenuFragment> fragmentRef = routeInfoMenu.findMenuFragment();
if (fragmentRef != null) {
routeMenuVisible = true;
MapRouteInfoMenuFragment f = fragmentRef.get();
if (landscapeLayout) {
tbw = (int) ((tb.getPixWidth() - f.getWidth()) * border);
} else {
tbh = (int) ((tb.getPixHeight() - f.getHeight()) * border);
dy = f.getHeight() - tbh / 2;
}
}
tb.setPixelDimensions(tbw, tbh);
double clat = bottom / 2 + top / 2;
//double clat = 5 * bottom / 4 - top / 4;
double clon = left / 2 + right / 2;
// landscape mode
// double clat = bottom / 2 + top / 2;
// double clon = 5 * left / 4 - right / 4;
tb.setLatLonCenter(clat, clon);
while (tb.getZoom() >= 7 && (!tb.containsLatLon(top, left) || !tb.containsLatLon(bottom, right))) {
tb.setZoom(tb.getZoom() - 1);
}
if (!landscapeLayout && routeMenuVisible) {
clat = tb.getLatFromPixel(tb.getPixWidth() / 2, tb.getPixHeight() / 2 + dy);
clon = tb.getLonFromPixel(tb.getPixWidth() / 2, tb.getPixHeight() / 2);
}
mapView.getAnimatedDraggingThread().startMoving(clat, clon, tb.getZoom(),
true);
}
}
}
@Override

View file

@ -3,7 +3,6 @@ package net.osmand.plus.base;
import android.content.Context;
import android.view.WindowManager;
import net.osmand.FloatMath;
import net.osmand.Location;
import net.osmand.StateChangedListener;
import net.osmand.ValueHolder;
@ -19,7 +18,6 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.AutoZoomMap;
import net.osmand.plus.R;
import net.osmand.plus.TargetPointsHelper.TargetPoint;
import net.osmand.plus.dashboard.DashboardOnMap;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.routing.RoutingHelper;
@ -28,8 +26,6 @@ import net.osmand.plus.views.AnimateDraggingMapThread;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.MapUtils;
import java.util.List;
public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLocationListener,
OsmAndCompassListener, IRouteInformationListener, MapMarkerChangedListener {
private static final int AUTO_FOLLOW_MSG_ID = OsmAndConstants.UI_HANDLER_LOCATION_SERVICE + 4;
@ -358,39 +354,6 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc
@Override
public void newRouteIsCalculated(boolean newRoute, ValueHolder<Boolean> showToast) {
RoutingHelper rh = app.getRoutingHelper();
if(newRoute && rh.isRoutePlanningMode() && mapView != null) {
RotatedTileBox rt = mapView.getCurrentRotatedTileBox();
Location lt = rh.getLastProjection();
if(lt == null) {
lt = app.getTargetPointsHelper().getPointToStartLocation();
}
if(lt != null) {
double left = lt.getLongitude(), right = lt.getLongitude();
double top = lt.getLatitude(), bottom = lt.getLatitude();
List<TargetPoint> list = app.getTargetPointsHelper().getIntermediatePointsWithTarget();
for(TargetPoint l : list) {
left = Math.min(left, l.getLongitude());
right = Math.max(right, l.getLongitude());
top = Math.max(top, l.getLatitude());
bottom = Math.min(bottom, l.getLatitude());
}
RotatedTileBox tb = new RotatedTileBox(rt);
tb.setPixelDimensions(5 * tb.getPixWidth() / 6, 5 * tb.getPixHeight() / 6);
double clat = 5 * bottom / 4 - top / 4;
double clon = left / 2 + right / 2;
// TODO for landscape menu
// double clat = bottom / 2 + top / 2;
// double clon = 5 * left / 4 - right / 4;
tb.setLatLonCenter(clat, clon);
while(tb.getZoom() >= 7 && (!tb.containsLatLon(top, left) || !tb.containsLatLon(bottom, right))) {
tb.setZoom(tb.getZoom() - 1);
}
mapView.getAnimatedDraggingThread().startMoving(clat, clon, tb.getZoom(),
true);
}
}
}
@Override

View file

@ -64,6 +64,22 @@ public class MapRouteInfoMenuFragment extends Fragment {
}
}
public int getHeight() {
if (mainView != null) {
return mainView.getHeight();
} else {
return 0;
}
}
public int getWidth() {
if (mainView != null) {
return mainView.getWidth();
} else {
return 0;
}
}
public void updateInfo() {
menu.updateInfo(mainView);
applyDayNightMode();
@ -98,9 +114,11 @@ public class MapRouteInfoMenuFragment extends Fragment {
public void applyDayNightMode() {
MapActivity ctx = getMapActivity();
boolean portrait = AndroidUiHelper.isOrientationPortrait(ctx);
boolean portraitMode = AndroidUiHelper.isOrientationPortrait(ctx);
boolean largeDevice = AndroidUiHelper.isXLargeDevice(ctx);
boolean landscapeLayout = !portraitMode && !largeDevice;
boolean nightMode = ctx.getMyApplication().getDaynightHelper().isNightModeForMapControls();
if (portrait) {
if (!landscapeLayout) {
AndroidUtils.setBackground(ctx, mainView, nightMode, R.drawable.bg_bottom_menu_light, R.drawable.bg_bottom_menu_dark);
} else {
AndroidUtils.setBackground(ctx, mainView, nightMode, R.drawable.bg_left_menu_light, R.drawable.bg_left_menu_dark);