history_location_manager: use new interface for tile info

Please note that there are some static objects in raw pointers with unclear
lifetime semantics. They are will be eliminated soon.
This commit is contained in:
leha-bot 2019-01-28 15:39:21 +03:00 committed by Alex
parent d18ff9ec8d
commit b2e70e63a2
1 changed files with 27 additions and 37 deletions

View File

@ -124,6 +124,25 @@ QString GoogleMapsLocationTileHelper::locationTileImageUrl(const LocationCoords
return url;
}
// This option could be enabled in core CMakeLists.txt
#ifdef KEPKA_USE_YANDEX_MAPS
using LocationMapTileHelper = YandexMapsLocationTileHelper;
#else
using LocationMapTileHelper = GoogleMapsLocationTileHelper;
#endif
//
// Static variables
//
namespace {
LocationManager *locationManager = nullptr;
ILocationMapTileHelper *locationMapTileHelper = nullptr;
} // namespace
//
// LocationClickHandler routines
//
QString LocationClickHandler::copyToClipboardContextItemText() const {
return lang(lng_context_copy_link);
}
@ -134,30 +153,18 @@ void LocationClickHandler::onClick(Qt::MouseButton button) const {
}
}
// This option could be enabled in core CMakeLists.txt
#ifdef KEPKA_USE_YANDEX_MAPS
void LocationClickHandler::setup() {
// Yandex.Maps accepts ll string in "longitude,latitude" format
auto latlon = _coords.lonAsString() + "%2C" + _coords.latAsString();
_text = qsl("https://maps.yandex.ru/?ll=") + latlon + qsl("&z=16");
_text = locationMapTileHelper->locationUrl(_coords);
}
#else
void LocationClickHandler::setup() {
auto latlon = _coords.latAsString() + ',' + _coords.lonAsString();
_text = qsl("https://maps.google.com/maps?q=") + latlon + qsl("&ll=") + latlon + qsl("&z=16");
}
#endif
namespace {
LocationManager *locationManager = nullptr;
} // namespace
void initLocationManager() {
if (!locationManager) {
locationManager = new LocationManager();
locationManager->init();
}
if (!locationMapTileHelper) {
locationMapTileHelper = new LocationMapTileHelper();
}
}
void reinitLocationManager() {
@ -172,6 +179,9 @@ void deinitLocationManager() {
delete locationManager;
locationManager = nullptr;
}
// if (ptr) is useless, because delete nullptr is valid.
delete locationMapTileHelper;
locationMapTileHelper = nullptr;
}
void LocationManager::init() {
@ -229,28 +239,8 @@ void LocationManager::getData(LocationData *data) {
w = convertScale(w);
h = convertScale(h);
}
#ifdef KEPKA_USE_YANDEX_MAPS
// see https://tech.yandex.ru/maps/doc/staticapi/1.x/dg/concepts/input_params-docpage/ for API parameters reference.
// Yandex.Maps accepts ll string in "longitude,latitude" format
auto coords = data->coords.lonAsString() + ',' + data->coords.latAsString();
const char *mapsApiUrl = "https://static-maps.yandex.ru/1.x/?ll=";
// red large marker looking like "9"
const char *mapsMarkerParams = ",pm2rdl";
// API format string
QString mapsApiParams = "&z=%1&size=%2,%3&l=map&scale=%4&pt=";
#else
auto coords = data->coords.latAsString() + ',' + data->coords.lonAsString();
const char *mapsApiUrl = "https://maps.googleapis.com/maps/api/staticmap?center=";
// additional marker params
const char *mapsMarkerParams = "&sensor=false";
// API format string with basic marker params (red and big)
QString mapsApiParams = "&zoom=%1&size=%2,%3&maptype=roadmap&scale=%4&markers=color:red|size:big|";
#endif
QString url = mapsApiUrl + coords + mapsApiParams.arg(zoom).arg(w).arg(h).arg(scale) + coords + mapsMarkerParams;
QString url = locationMapTileHelper->locationTileImageUrl(data->coords, w, h, zoom, scale);
QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url)));
imageLoadings[reply] = data;
}