Skip to content

Commit

Permalink
Add optional support for Yandex.Maps static API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leha-bot committed Dec 31, 2018
1 parent 419705e commit 17e2bda
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 27 additions & 3 deletions Telegram/SourceFiles/history/history_location_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 17e2bda

Please sign in to comment.