void QDeclarativeGeoMap::fitViewportToGeoShape() { double bboxWidth; double bboxHeight; QGeoCoordinate centerCoordinate; switch (m_region.type()) { case QGeoShape::RectangleType: { QGeoRectangle rect = m_region; QDoubleVector2D topLeftPoint = m_map->coordinateToItemPosition(rect.topLeft(), false); QDoubleVector2D botRightPoint = m_map->coordinateToItemPosition(rect.bottomRight(), false); bboxWidth = qAbs(topLeftPoint.x() - botRightPoint.x()); bboxHeight = qAbs(topLeftPoint.y() - botRightPoint.y()); centerCoordinate = rect.center(); break; } case QGeoShape::CircleType: { QGeoCircle circle = m_region; centerCoordinate = circle.center(); QGeoCoordinate edge = centerCoordinate.atDistanceAndAzimuth(circle.radius(), 90); QDoubleVector2D centerPoint = m_map->coordinateToItemPosition(centerCoordinate, false); QDoubleVector2D edgePoint = m_map->coordinateToItemPosition(edge, false); bboxWidth = qAbs(centerPoint.x() - edgePoint.x()) * 2; bboxHeight = bboxWidth; break; } case QGeoShape::UnknownType: //Fallthrough to default default: return; } // position camera to the center of bounding box setProperty("center", QVariant::fromValue(centerCoordinate)); //If the shape is empty we just change centerposition, not zoom if (bboxHeight == 0 && bboxWidth == 0) return; // adjust zoom double bboxWidthRatio = bboxWidth / (bboxWidth + bboxHeight); double mapWidthRatio = width() / (width() + height()); double zoomRatio; if (bboxWidthRatio > mapWidthRatio) zoomRatio = bboxWidth / width(); else zoomRatio = bboxHeight / height(); qreal newZoom = std::log10(zoomRatio) / std::log10(0.5); newZoom = std::floor(qMax(minimumZoomLevel(), (m_map->mapController()->zoom() + newZoom))); setProperty("zoomLevel", QVariant::fromValue(newZoom)); }
/*! \qmlmethod QtLocation::Map::fitViewportToGeoShape(QGeoShape shape) Fits the current viewport to the boundary of the shape. The camera is positioned in the center of the shape, and at the largest integral zoom level possible which allows the whole shape to be visible on screen */ void QDeclarativeGeoMap::fitViewportToGeoShape(const QVariant &variantShape) { if (!map_ || !mappingManagerInitialized_) return; QGeoShape shape; if (variantShape.userType() == qMetaTypeId<QGeoRectangle>()) shape = variantShape.value<QGeoRectangle>(); else if (variantShape.userType() == qMetaTypeId<QGeoCircle>()) shape = variantShape.value<QGeoCircle>(); else if (variantShape.userType() == qMetaTypeId<QGeoShape>()) shape = variantShape.value<QGeoShape>(); if (!shape.isValid()) return; double bboxWidth; double bboxHeight; QGeoCoordinate centerCoordinate; switch (shape.type()) { case QGeoShape::RectangleType: { QGeoRectangle rect = shape; QDoubleVector2D topLeftPoint = map_->coordinateToScreenPosition(rect.topLeft(), false); QDoubleVector2D botRightPoint = map_->coordinateToScreenPosition(rect.bottomRight(), false); bboxWidth = qAbs(topLeftPoint.x() - botRightPoint.x()); bboxHeight = qAbs(topLeftPoint.y() - botRightPoint.y()); centerCoordinate = rect.center(); break; } case QGeoShape::CircleType: { QGeoCircle circle = shape; centerCoordinate = circle.center(); QGeoCoordinate edge = centerCoordinate.atDistanceAndAzimuth(circle.radius(), 90); QDoubleVector2D centerPoint = map_->coordinateToScreenPosition(centerCoordinate, false); QDoubleVector2D edgePoint = map_->coordinateToScreenPosition(edge, false); bboxWidth = qAbs(centerPoint.x() - edgePoint.x()) * 2; bboxHeight = bboxWidth; break; } case QGeoShape::UnknownType: //Fallthrough to default default: return; } // position camera to the center of bounding box setProperty("center", QVariant::fromValue(centerCoordinate)); //If the shape is empty we just change centerposition, not zoom if (bboxHeight == 0 && bboxWidth == 0) return; // adjust zoom double bboxWidthRatio = bboxWidth / (bboxWidth + bboxHeight); double mapWidthRatio = width() / (width() + height()); double zoomRatio; if (bboxWidthRatio > mapWidthRatio) zoomRatio = bboxWidth / width(); else zoomRatio = bboxHeight / height(); qreal newZoom = log10(zoomRatio) / log10(0.5); newZoom = floor(qMax(minimumZoomLevel(), (map_->mapController()->zoom() + newZoom))); setProperty("zoomLevel", QVariant::fromValue(newZoom)); }