Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Migrate to Yandex Locator API
  • Loading branch information
neochapay committed Oct 31, 2020
1 parent c756c0c commit 3cf98c0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
36 changes: 22 additions & 14 deletions plugin/yandexonlinelocator.cpp
Expand Up @@ -214,13 +214,8 @@ QPair<QDateTime, QVariantMap> YandexOnlineLocator::buildLocationQuery(

bool YandexOnlineLocator::findLocation(const QPair<QDateTime, QVariantMap> &query)
{
if (query.first.isNull() || query.second.isEmpty()) {
qDebug() << "Empty query data provided";
return false;
}

if (!loadYandexKey()) {
qDebug() << "Unable to load MLS API key";
qDebug() << "Unable to load Yandex API key";
return false;
}

Expand All @@ -244,12 +239,22 @@ bool YandexOnlineLocator::findLocation(const QPair<QDateTime, QVariantMap> &quer
}
}

QNetworkRequest req(QUrl("https://location.services.mozilla.com/v1/geolocate?key=" + m_yandexKey));
QNetworkRequest req(QUrl("http://api.lbs.yandex.net/geolocation"));
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

const QJsonDocument doc = QJsonDocument::fromVariant(query.second);
const QByteArray json = doc.toJson();
m_currentReply = m_nam->post(req, json);
QJsonObject doc;

QJsonObject common;
common["version"] = "1.0";
common["api_key"] = m_yandexKey;
doc.insert("common",common);

/*TODO ADD CELLID and WiFi*/
Q_UNUSED(query);

const QByteArray json = QJsonDocument(doc).toJson();

m_currentReply = m_nam->post(req, "json="+json);
if (m_currentReply->error() != QNetworkReply::NoError) {
qDebug() << "POST request failed:" << m_currentReply->errorString();
return false;
Expand Down Expand Up @@ -311,23 +316,26 @@ bool YandexOnlineLocator::readServerResponseData(const QByteArray &data, QString
}

QJsonObject obj = json.object();
QVariantMap location = obj.value("location").toVariant().toMap();
QVariantMap location = obj.value("position").toVariant().toMap();
if (location.isEmpty()) {
*errorString = "JSON parse error: no location data found in " + data;
return false;
}

bool latitudeOk = false;
bool longitudeOk = false;
double latitude = location["lat"].toDouble(&latitudeOk);
double longitude = location["lng"].toDouble(&longitudeOk);
double latitude = location["latitude"].toDouble(&latitudeOk);
double longitude = location["longitude"].toDouble(&longitudeOk);
if (!latitudeOk || !longitudeOk) {
*errorString = "JSON parse error: latitude or longitude not readable in " + data;
return false;
}

bool accuracyOk = false;
double accuracy = obj.value("accuracy").toVariant().toDouble(&accuracyOk);

// Yandex locator dont have accuracy
double accuracy = 0.0;
//double accuracy = obj.value("accuracy").toVariant().toDouble(&accuracyOk);
if (!accuracyOk) {
accuracy = -1;
}
Expand Down
7 changes: 5 additions & 2 deletions plugin/yandexprovider.cpp
Expand Up @@ -109,7 +109,7 @@ YandexProvider::YandexProvider(QObject *parent)
new GeoclueAdaptor(this);
new PositionAdaptor(this);

qDebug() << "Mozilla Location Services geoclue plugin active";
qDebug() << "Yandex Location Services geoclue plugin active";
if (m_watchedServices.isEmpty()) {
m_idleTimer.start(QuitIdleTime, this);
}
Expand Down Expand Up @@ -284,10 +284,13 @@ int YandexProvider::GetPosition(int &timestamp, double &latitude, double &longit

void YandexProvider::timerEvent(QTimerEvent *event)
{
//DEBUG
calculatePositionAndEmitLocation();

if (event->timerId() == m_idleTimer.timerId()) {
m_idleTimer.stop();
qDebug() << "have been idle for too long, quitting";
qApp->quit();
// qApp->quit();
} else if (event->timerId() == m_fixLostTimer.timerId()) {
m_fixLostTimer.stop();
setStatus(StatusAcquiring);
Expand Down

0 comments on commit 3cf98c0

Please sign in to comment.