From 17e2bda4041f5841c496569c2544837db0c1eeaa Mon Sep 17 00:00:00 2001 From: leha-bot Date: Tue, 2 Oct 2018 12:35:26 +0300 Subject: [PATCH] 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. --- CMakeLists.txt | 9 ++++++ .../history/history_location_manager.cpp | 30 +++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b6949e707..e17924f62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/Telegram/SourceFiles/history/history_location_manager.cpp b/Telegram/SourceFiles/history/history_location_manager.cpp index 0252cad8c..88892f51f 100644 --- a/Telegram/SourceFiles/history/history_location_manager.cpp +++ b/Telegram/SourceFiles/history/history_location_manager.cpp @@ -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; }