ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

如何在Windows上使用Qt c从一个地理地址获取纬度/经度?

2019-10-02 10:06:08  阅读:689  来源: 互联网

标签:c qt geolocation location coordinates


它可以在Qt c上使用地理位置(国家,州,城市,街道等等)获取地理位置的坐标(纬度和经度).

我知道库QGeoCoordinates,QGeoLocation和QGeoAddress,但我不知道是否可以通过它们从地址获取坐标.

欢迎各方面的帮助.

解决方法:

我需要一些时间来掌握这个,但最后我得到了它的工作.

起初我尝试了你提到的课程:QGeoAddress,QGeoLocation,QGeoCoordinate

// standard C++ header:
#include <iostream>

// Qt header:
#include <QGeoAddress>
#include <QGeoCoordinate>
#include <QGeoLocation>

using namespace std;

int main()
{
  // build address
  QGeoAddress qGeoAddr;
  qGeoAddr.setCountry(QString::fromUtf8("Germany"));
  qGeoAddr.setPostalCode(QString::fromUtf8("88250"));
  qGeoAddr.setCity(QString::fromUtf8("Weingarten"));
  qGeoAddr.setStreet(QString::fromUtf8("Heinrich-Hertz-Str. 6"));
  QGeoLocation qGeoLoc.setAddress(qGeoAddr);
  QGeoCoordinate qGeoCoord = qGeoLoc.coordinate();
  cout
    << "Lat.:  " << qGeoCoord.latitude() << endl
    << "Long.: " << qGeoCoord.longitude() << endl
    << "Alt.:  " << qGeoCoord.altitude() << endl;
  return 0;
}

这编译并运行,但输出不是很享受:

$./testQGeoAddress
Qt Version: 5.6.2
Lat.:  nan
Long.: nan
Alt.:  nan

那么,代码有什么问题?回过头来思考一下这个我得到了一个线索:有一些服务缺失,可以将地址转换为坐标.这可能是什么? maps.google.com或类似的东西?

我用Google搜索了一段时间,最后找到了关于Qt位置库的点击.经过实验和摆弄一段时间后,我终于得到了这个运行的例子:

文件testQGeoAddress.pro:

SOURCES = testQGeoAddress.cc

QT += widgets
QT += positioning
QT += location

文件testQGeoAddress.cc:

// standard C++ header:
#include <iostream>
#include <string>

// Qt header:
#include <QApplication>
#include <QGeoAddress>
#include <QGeoCodingManager>
#include <QGeoCoordinate>
#include <QGeoLocation>
#include <QGeoServiceProvider>

using namespace std;

int main(int argc, char **argv)
{
  cout << "Qt Version: " << QT_VERSION_STR << endl;
  // main application
#undef qApp // undef macro qApp out of the way
  QCoreApplication qApp(argc, argv);
  // check for available services
  QStringList qGeoSrvList
    = QGeoServiceProvider::availableServiceProviders();
  for (QString entry : qGeoSrvList) {
    cout << "Try service: " << entry.toStdString() << endl;
    // choose provider
    QGeoServiceProvider qGeoService(entry);
    QGeoCodingManager *pQGeoCoder = qGeoService.geocodingManager();
    if (!pQGeoCoder) {
      cerr
        << "GeoCodingManager '" << entry.toStdString()
        << "' not available!" << endl;
      continue;
    }
    QLocale qLocaleC(QLocale::C, QLocale::AnyCountry);
    pQGeoCoder->setLocale(qLocaleC);
    // build address
    QGeoAddress qGeoAddr;
    qGeoAddr.setCountry(QString::fromUtf8("Germany"));
    qGeoAddr.setPostalCode(QString::fromUtf8("88250"));
    qGeoAddr.setCity(QString::fromUtf8("Weingarten"));
    qGeoAddr.setStreet(QString::fromUtf8("Heinrich-Hertz-Str. 6"));
    QGeoCodeReply *pQGeoCode = pQGeoCoder->geocode(qGeoAddr);
    if (!pQGeoCode) {
      cerr << "GeoCoding totally failed!" << endl;
      continue;
    }
    cout << "Searching..." << endl;
    QObject::connect(pQGeoCode, &QGeoCodeReply::finished,
      [&qApp, &qGeoAddr, pQGeoCode](){
        cout << "Reply: " << pQGeoCode->errorString().toStdString() << endl;
        switch (pQGeoCode->error()) {
#define CASE(ERROR) \
case QGeoCodeReply::ERROR: cerr << #ERROR << endl; break
          CASE(NoError);
          CASE(EngineNotSetError);
          CASE(CommunicationError);
          CASE(ParseError);
          CASE(UnsupportedOptionError);
          CASE(CombinationError);
          CASE(UnknownError);
#undef CASE
          default: cerr << "Undocumented error!" << endl;
        }
        if (pQGeoCode->error() != QGeoCodeReply::NoError) return;
        // eval. result
        QList<QGeoLocation> qGeoLocs = pQGeoCode->locations();
        cout << qGeoLocs.size() << " location(s) returned." << endl;
        for (QGeoLocation &qGeoLoc : qGeoLocs) {
          qGeoLoc.setAddress(qGeoAddr);
          QGeoCoordinate qGeoCoord = qGeoLoc.coordinate();
          cout
            << "Lat.:  " << qGeoCoord.latitude() << endl
            << "Long.: " << qGeoCoord.longitude() << endl
            << "Alt.:  " << qGeoCoord.altitude() << endl;
        }
        qApp.exit(0);
      });
    return qApp.exec();
  }
  return 0;
}

在Windows 10(64位)上使用gg在cygwin中构建:

$qmake-qt5 

$make
g++ -c -pipe -fno-keep-inline-dllexport -O2 -std=gnu++11 -Wall -W -D_REENTRANT -fexceptions -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_LOCATION_LIB -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_POSITIONING_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I. -I/usr/include/qt5 -I/usr/include/qt5/QtWidgets -I/usr/include/qt5/QtLocation -I/usr/include/qt5/QtQuick -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtPositioning -I/usr/include/qt5/QtQml -I/usr/include/qt5/QtNetwork -I/usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQGeoAddress.o testQGeoAddress.cc
g++  -o testQGeoAddress.exe testQGeoAddress.o   -lQt5Widgets -lQt5Location -lQt5Quick -lQt5Gui -lQt5Positioning -lQt5Qml -lQt5Network -lQt5Core -lGL -lpthread 

$./testQGeoAddress.exe
Qt Version: 5.6.2
Try service: mapbox
GeoCodingManager 'mapbox' not available!
Try service: osm
Searching...
Reply: 
NoError
1 location(s) returned.
Lat.:  47.8198
Long.: 9.63105
Alt.:  nan

$

第一次尝试不错.关于QGeoCoordinate :: altitude()值:这可能无法从Open Street Map(osm)获得.

要检查结果是否正确,我在maps.google.com中解析了输出坐标:

Search 47.8198, 9.63105 in maps.google.com

红色气球是搜索的结果.我在GIMP中添加了蓝点来表达正确的位置.那么,搜索接近正确的位置.差异可能是由于坐标精度有限……

标签:c,qt,geolocation,location,coordinates
来源: https://codeday.me/bug/20191002/1842508.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有