From 1d80b08a8fb1fe06fd5cf60eac43b9ae1dc1197c Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Mon, 27 Nov 2017 17:05:43 +0200 Subject: mapwidget.qml: more minor improvements - Add the helper coordIsValid() and use it in centerOnCoordinate(). This checks for NaN coordinates and prevents an infinite while() loop. - Use '-= 1.0' instead of '--' to be more clear that the decremented variable is a float - Store the current center with 'centerStored' in centerOnCoordinate(). Fixes a possible zoom out glitch. - use Math.floor() to convert 'zoomLevel' to integer when estimating zoomOut in centerOn*() Signed-off-by: Lubomir I. Ivanov --- map-widget/qml/MapWidget.qml | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'map-widget') diff --git a/map-widget/qml/MapWidget.qml b/map-widget/qml/MapWidget.qml index 7b122babe..12b4554eb 100644 --- a/map-widget/qml/MapWidget.qml +++ b/map-widget/qml/MapWidget.qml @@ -136,19 +136,28 @@ Item { return !isNaN(pt.x) } + function coordIsValid(coord) { + if (coord == null || isNaN(coord.latitude) || isNaN(coord.longitude) || + (coord.latitude === 0.0 && coord.longitude === 0.0)) + return false; + return true; + } + function stopZoomAnimations() { mapAnimationZoomIn.stop() } function centerOnCoordinate(coord) { stopZoomAnimations() - if (coord.latitude === 0.0 && coord.longitude === 0.0) { - // Do nothing + if (!coordIsValid(coord)) { + console.warn("MapWidget.qml: centerOnCoordinate(): !coordIsValid()") } else { var newZoomOutFound = false var zoomStored = zoomLevel + var centerStored = QtPositioning.coordinate(center.latitude, center.longitude) newZoomOut = zoomLevel newCenter = coord + zoomLevel = Math.floor(zoomLevel) while (zoomLevel > minimumZoomLevel) { var pt = fromCoordinate(coord) if (pointIsVisible(pt)) { @@ -156,11 +165,12 @@ Item { newZoomOutFound = true break } - zoomLevel-- + zoomLevel -= 1.0 } if (!newZoomOutFound) newZoomOut = defaultZoomOut zoomLevel = zoomStored + center = centerStored newZoom = zoomStored mapAnimationZoomIn.restart() } @@ -185,13 +195,13 @@ Item { newZoomOutFound = true break } - zoomLevel-- + zoomLevel -= 1.0 } if (!newZoomOutFound) newZoomOut = defaultZoomOut // calculate zoom in center = newCenter - zoomLevel = maximumZoomLevel + zoomLevel = Math.floor(maximumZoomLevel) var diagonalRect = topLeft.distanceTo(bottomRight) while (zoomLevel > minimumZoomLevel) { var c0 = toCoordinate(Qt.point(0.0, 0.0)) @@ -200,7 +210,7 @@ Item { newZoom = zoomLevel - 2.0 break } - zoomLevel-- + zoomLevel -= 1.0 } if (newZoom > defaultZoomIn) newZoom = defaultZoomIn -- cgit v1.2.3-70-g09d2