Fix map centering

This commit is contained in:
Alexey Kulish 2016-02-23 11:53:29 +03:00
parent f047aa9ceb
commit 6511a140fd
2 changed files with 44 additions and 31 deletions

View file

@ -1190,7 +1190,6 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents,
public void newRouteIsCalculated(boolean newRoute, ValueHolder<Boolean> showToast) { public void newRouteIsCalculated(boolean newRoute, ValueHolder<Boolean> showToast) {
RoutingHelper rh = app.getRoutingHelper(); RoutingHelper rh = app.getRoutingHelper();
if (newRoute && rh.isRoutePlanningMode() && mapView != null) { if (newRoute && rh.isRoutePlanningMode() && mapView != null) {
RotatedTileBox rt = mapView.getCurrentRotatedTileBox();
Location lt = rh.getLastProjection(); Location lt = rh.getLastProjection();
if (lt == null) { if (lt == null) {
lt = app.getTargetPointsHelper().getPointToStartLocation(); lt = app.getTargetPointsHelper().getPointToStartLocation();
@ -1198,51 +1197,29 @@ public class MapActivity extends AccessibleActivity implements DownloadEvents,
if (lt != null) { if (lt != null) {
double left = lt.getLongitude(), right = lt.getLongitude(); double left = lt.getLongitude(), right = lt.getLongitude();
double top = lt.getLatitude(), bottom = lt.getLatitude(); double top = lt.getLatitude(), bottom = lt.getLatitude();
List<TargetPoint> list = app.getTargetPointsHelper().getIntermediatePointsWithTarget(); List<Location> list = rh.getCurrentCalculatedRoute();
for (TargetPoint l : list) { for (Location l : list) {
left = Math.min(left, l.getLongitude()); left = Math.min(left, l.getLongitude());
right = Math.max(right, l.getLongitude()); right = Math.max(right, l.getLongitude());
top = Math.max(top, l.getLatitude()); top = Math.max(top, l.getLatitude());
bottom = Math.min(bottom, l.getLatitude()); bottom = Math.min(bottom, l.getLatitude());
} }
RotatedTileBox tb = new RotatedTileBox(rt);
double border = 0.8; RotatedTileBox tb = mapView.getCurrentRotatedTileBox().copy();
int dy = 0; int tileBoxWidthPx = 0;
int tileBoxHeightPx = 0;
MapRouteInfoMenu routeInfoMenu = mapLayers.getMapControlsLayer().getMapRouteInfoMenu(); 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(); WeakReference<MapRouteInfoMenuFragment> fragmentRef = routeInfoMenu.findMenuFragment();
if (fragmentRef != null) { if (fragmentRef != null) {
routeMenuVisible = true;
MapRouteInfoMenuFragment f = fragmentRef.get(); MapRouteInfoMenuFragment f = fragmentRef.get();
if (landscapeLayout) { if (landscapeLayout) {
tbw = (int) ((tb.getPixWidth() - f.getWidth()) * border); tileBoxWidthPx = tb.getPixWidth() - f.getWidth();
} else { } else {
tbh = (int) ((tb.getPixHeight() - f.getHeight()) * border); tileBoxHeightPx = tb.getPixHeight() - f.getHeight();
dy = f.getHeight() - tbh / 2;
} }
} }
tb.setPixelDimensions(tbw, tbh); mapView.fitRectToMap(left, right, top, bottom, tileBoxWidthPx, tileBoxHeightPx, 0);
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);
} }
} }
} }

View file

@ -760,6 +760,42 @@ public class OsmandMapTileView implements IMapDownloaderCallback {
} }
public void fitRectToMap(double left, double right, double top, double bottom,
int tileBoxWidthPx, int tileBoxHeightPx, int marginTopPx) {
RotatedTileBox tb = currentViewport.copy();
double border = 0.8;
int dy = 0;
int tbw = (int) (tb.getPixWidth() * border);
int tbh = (int) (tb.getPixHeight() * border);
if (tileBoxWidthPx > 0) {
tbw = (int) (tileBoxWidthPx * border);
} else if (tileBoxHeightPx > 0) {
tbh = (int) (tileBoxHeightPx * border);
dy = (tb.getPixHeight() - tileBoxHeightPx) / 2 - marginTopPx;
}
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() < 17 && tb.containsLatLon(top, left) && tb.containsLatLon(bottom, right)) {
tb.setZoom(tb.getZoom() + 1);
}
while (tb.getZoom() >= 7 && (!tb.containsLatLon(top, left) || !tb.containsLatLon(bottom, right))) {
tb.setZoom(tb.getZoom() - 1);
}
if (dy != 0) {
clat = tb.getLatFromPixel(tb.getPixWidth() / 2, tb.getPixHeight() / 2 + dy);
clon = tb.getLonFromPixel(tb.getPixWidth() / 2, tb.getPixHeight() / 2);
}
animatedDraggingThread.startMoving(clat, clon, tb.getZoom(), true);
}
public boolean onGenericMotionEvent(MotionEvent event) { public boolean onGenericMotionEvent(MotionEvent event) {
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0 && if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0 &&
event.getAction() == MotionEvent.ACTION_SCROLL && event.getAction() == MotionEvent.ACTION_SCROLL &&