2017-04-06 13 views
1

在Qt C++中,只需使用地理位置(國家,州,城市,街道等)就可以獲得地理位置的座標(經度和緯度)?如何從windows上使用Qt C++獲取一個地理位置的緯度/經度?

我知道庫QGeoCoordinates,QGeoLocation和QGeoAddress,但我不知道是否可以通過它們從地址獲取座標。

歡迎任何幫助。

+0

你是什麼意思的地址?像IP地址一樣? – maximdumont

+0

@maximdumont地理位置:國家,州,城市,街道等... –

+0

https://www.qtdeveloperdays。com/2013/sites/default/files/presentation_pdf/ArcGIS_presentation.pdf,可能嗎? – maximdumont

回答

1

我需要一些時間來掌握這一點,但我終於得到它的工作。

起初,我試圖通過你提到的類:QGeoLocationQGeoCoordinateQGeoAddress,:

// 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 location庫的點擊。試驗和擺弄了一陣後,我終於得到這個運行的例子:

文件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; 
} 

建立與G ++中的cygwin在Windows 10(64位) :

$ 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()價值:這可能無法從開放街道地圖(osm)獲得。

要檢查結果是否正確,我決定在maps.google.com輸出座標:

Search 47.8198, 9.63105 in maps.google.com

紅氣球是搜索的結果。我在GIMP中添加了藍點以表示正確的位置。那麼,搜索接近正確的位置。這種差異可能是由於座標精度有限造成的...

+0

謝謝,這對我來說非常有效。 –

1

我認爲這是可能的。我從來沒有嘗試過。 請用下面的代碼嘗試。

注:地址屬性是歸美國特徵名稱,可以映射到本地功能級別(例如國家‘Bundesland’在德國的比賽)。(複製表格文檔)

//Create a geo address object. 

QGeoAddress *add = new QGeoAddress(); 

//Assumed for USA. 
//Try below order so that search may be bit quicker. 
add->setCountry("Country name"); 
add->setCountryCode("country code"); 
add->setState("state"); 
add->setCity("city name"); 
add->setCounty("county name"); 
add->setStreet("street name") 
add->setPostalCode("Zip Code") 


//Create a geo location object 

QGeoLocation *loc = new QGeoLocation(); 
QGeoLocation::setAddress(add); //Set the QGeoLocation object 

//Get the coordinates by QGeoCoordinate 
QGeoCoordinate cord = loc->coordinate(); 

//Now get the coordinates for latitude and longitude. 
double latitude = cord.latitude(); 
doublelongitude = cord.longitude(); 
+0

謝謝,我會嘗試! –

+0

忘了告訴...你應該建立「qtlocation」庫。 – Naidu