Add optional support for Yandex.Maps static API

You could enable it via CMake option KEPKA_OPTION_USE_YANDEX_MAPS:
cmake -DKEPKA_OPTION_USE_YANDEX_MAPS=ON ..

It is used instead of Google Maps API for locations and on location click.
Sometimes Google Maps return no maps data. It is related with recent free
API usages count decrease from 750k requests to 28k (according to
https://habr.com/post/417715/).

The code on this commit should be cleaned up and rewritten using Maps API
string builders.
This commit is contained in:
leha-bot 2018-10-02 12:35:26 +03:00
parent 419705ed15
commit 17e2bda404
2 changed files with 36 additions and 3 deletions

View File

@ -13,6 +13,15 @@ if(PROJECT_VERSION_RC)
set(PROJECT_VERSION "${PROJECT_VERSION}-rc${PROJECT_VERSION_RC}")
endif()
##================================================
## Build customizations
##================================================
option(KEPKA_OPTION_USE_YANDEX_MAPS "Use Yandex.Maps static API for locations (instead Google Maps)" OFF)
if(KEPKA_OPTION_USE_YANDEX_MAPS)
add_definitions("-DKEPKA_USE_YANDEX_MAPS")
endif()
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/modules/")
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.10)

View File

@ -48,10 +48,20 @@ 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");
}
#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;
@ -133,14 +143,28 @@ 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.
auto coords = data->coords.lonAsString() + ',' + data->coords.latAsString(); // Yandex.Maps accepts ll string in "longitude,latitude" format
const char *mapsApiUrl = "https://static-maps.yandex.ru/1.x/?ll=";
QString mapsApiParams = "&z=%1&size=%2,%3&l=map&scale=%4&pt=";
const char *mapsMarkerParams = ",pm2rdl"; // red large marker looking like "9"
#else
auto coords = data->coords.latAsString() + ',' + data->coords.lonAsString();
QString url = qsl("https://maps.googleapis.com/maps/api/staticmap?center=") + coords +
qsl("&zoom=%1&size=%2x%3&maptype=roadmap&scale=%4&markers=color:red|size:big|")
const char *mapsApiUrl = "https://maps.googleapis.com/maps/api/staticmap?center=";
QString mapsApiParams = "&zoom=%1&size=%2,%3&maptype=roadmap&scale=%4&markers=color:red|size:big|";
const char *mapsMarkerParams = "&sensor=false";
#endif
QString url = // qsl("https://maps.googleapis.com/maps/api/staticmap?center=") + coords +
// qsl("&zoom=%1&size=%2x%3&maptype=roadmap&scale=%4&markers=color:red|size:big|")
mapsApiUrl + coords + mapsApiParams
.arg(zoom)
.arg(w)
.arg(h)
.arg(scale) +
coords + qsl("&sensor=false");
coords
+ mapsMarkerParams;
//+ qsl("&sensor=false");
QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url)));
imageLoadings[reply] = data;
}