c++ - QGeoCodingManager 没有错误但没有结果

标签 c++ qt qt5 qt5.7

我正在尝试获取 QGeoLocation。我的 Qt 版本是 5.7.1,顺便说一下,我在 Debian 上运行它。

我看到了这个帖子 how to get latitude/longitude from one geo address using Qt c++ on windows?

我从 Scheff 的回答中复制并粘贴了有效的解决方案,但仍然没有错误和 0 个位置。这与我的设置/环境有关吗?

这段较短的代码具有相同的效果:

#include <QApplication>
#include <QGeoAddress>
#include <QGeoCodingManager>
#include <QGeoCoordinate>
#include <QGeoLocation>
#include <QGeoServiceProvider>
#include <QtDebug>

int main( int argc, char **argv)
{
    QCoreApplication app( argc, argv );

    QGeoServiceProvider geoSrv( "osm" );
    QGeoCodingManager *geoCoder = geoSrv.geocodingManager();
    QGeoAddress addr;
    addr.setCountry( "China" );
    QGeoCodeReply *geoCode = geoCoder->geocode( addr );

    if ( geoCode->error() )
        qDebug() << "error";

    qDebug() << geoCode->locations().length();

    return app.exec();
}

最佳答案

我在遇到同样的问题时发现了您的帖子。对我来说,QGeoServiceProvider 代码突然停止使用 OpenStreetmap。我很快尝试了“这里”的 api,它似乎可以使用完全相同的代码。通过使用 wireshark 进行一些快速检查,我很容易发现问题。

QGeoServiceProvider 尝试通过此 url 连接到 OpenStreetMap api:http://nominatim.openstreetmap.org它通过 HTTP 303 重定向到 https://nominatim.openstreetmap.org .显然,QGeoServiceProvider 无法正确处理此重定向。我通过在 osm.geocoding.host 参数中提供新的 url 来修复它。使用您的代码,这看起来像这样:

#include <QApplication>
#include <QGeoAddress>
#include <QGeoCodingManager>
#include <QGeoCoordinate>
#include <QGeoLocation>
#include <QGeoServiceProvider>
#include <QtDebug>

int main( int argc, char **argv)
{
   QCoreApplication app( argc, argv );

   //Add this
   QMap<QString,QVariant> params;
   params["osm.geocoding.host"] = "https://nominatim.openstreetmap.org";

   QGeoServiceProvider geoSrv( "osm", params );

   QGeoCodingManager *geoCoder = geoSrv.geocodingManager();
   QGeoAddress addr;
   addr.setCountry( "China" );
   QGeoCodeReply *geoCode = geoCoder->geocode( addr );

   if ( geoCode->error() )
       qDebug() << "error";

   qDebug() << geoCode->locations().length();

   return app.exec();
}

希望这对您有所帮助!

关于c++ - QGeoCodingManager 没有错误但没有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50548492/

相关文章:

c++ - CGAL二次规划求解器,如何在目标函数中输入 "x^4"?并在限制条件下?

c++ - 是否可以为 std::cin 设置超时?

c++ - 使用 C++ 在不同选项卡中显示相同 QWidget 的 QTabWidget 或 QTabBar

c++ - QSettings : is there a limit in . ini文件行长度?

QML:contentItem 不显示任何图像

c++ - 使用可以在多个不同位置定义的函数之类的 C++ 类

c++ - 如何将数字的每个数字向右排列一步?

c++ - Qt:使用uic生成ui_类,通过类名动态加载

c++ - 不完整类型的无效使用/前向声明

android - 显示半透明圆圈的快速动画