diff --git a/src/MissionManager/MissionController.cc b/src/MissionManager/MissionController.cc index 1e19e2e890758539bfb2c6a31fb9dc2c65995de7..03bf70e4e9f1467d7c2297c6609d0268a67db5dc 100644 --- a/src/MissionManager/MissionController.cc +++ b/src/MissionManager/MissionController.cc @@ -1208,31 +1208,43 @@ double MissionController::_normalizeLon(double lon) /// Add the home position item to the front of the list void MissionController::_addPlannedHomePosition(QmlObjectListModel* visualItems, bool addToCenter) { + bool homePositionSet = false; + SimpleMissionItem* homeItem = new SimpleMissionItem(_activeVehicle, this); visualItems->insert(0, homeItem); if (visualItems->count() > 1 && addToCenter) { - VisualMissionItem* item = qobject_cast(visualItems->get(1)); - - double north = _normalizeLat(item->coordinate().latitude()); - double south = north; - double east = _normalizeLon(item->coordinate().longitude()); - double west = east; - - for (int i=2; icount(); i++) { - item = qobject_cast(visualItems->get(i)); + double north, south, east, west; + bool firstCoordSet = false; - double lat = _normalizeLat(item->coordinate().latitude()); - double lon = _normalizeLon(item->coordinate().longitude()); + for (int i=1; icount(); i++) { + VisualMissionItem* item = qobject_cast(visualItems->get(i)); + + if (item->specifiesCoordinate()) { + if (firstCoordSet) { + double lat = _normalizeLat(item->coordinate().latitude()); + double lon = _normalizeLon(item->coordinate().longitude()); + north = fmax(north, lat); + south = fmin(south, lat); + east = fmax(east, lon); + west = fmin(west, lon); + } else { + firstCoordSet = true; + north = _normalizeLat(item->coordinate().latitude()); + south = north; + east = _normalizeLon(item->coordinate().longitude()); + west = east; + } + } + } - north = fmax(north, lat); - south = fmin(south, lat); - east = fmax(east, lon); - west = fmin(west, lon); + if (firstCoordSet) { + homePositionSet = true; + homeItem->setCoordinate(QGeoCoordinate((south + ((north - south) / 2)) - 90.0, (west + ((east - west) / 2)) - 180.0, 0.0)); } + } - homeItem->setCoordinate(QGeoCoordinate((south + ((north - south) / 2)) - 90.0, (west + ((east - west) / 2)) - 180.0, 0.0)); - } else { + if (!homePositionSet) { homeItem->setCoordinate(qgcApp()->lastKnownHomePosition()); } }