class DialogMap
: public QDialog,
private Ui
::DialogMap {
Q_OBJECT
public:
//functions:
//variables:
private slots:
void calcMap();
void examineWait();
void examinePage();
private:
bool examineSuccess;
int examineTries;
bool examinetimedout;
};
//setup interface of main window:
examineSuccess = false;
examinetimedout = false;
examineTries = 10;
setupUi(this);
//if we click on the button in the GUI we call that slot:
connect(calcButton, SIGNAL(clicked()), this, SLOT(calcMap()));
//this is to make sure the program examines the script after the map is first loaded:
connect(webView, SIGNAL(loadFinished(bool)), this, SLOT(examineWait()));
///////////////////////////////////////////////////////////////////
//also can have other slots here, such as a "clicked" signal connecting to examinewait
//for when the map is clicked by the user
//or any other signal that might trigger you to examine the lat/lng in the map
//////////////////////////////////////////////////////////////////////
//load the view with the map page:
webView
->setUrl
(QUrl("http://domain/map.html"));
}
//make the map process the GUi vars we pass to the javascript:
void DialogMap::calcMap() {
//reset success to false because we have not examined it yet:
examineSuccess = false;
examinetimedout = false;
examineTries = 10;
//input the vars from our GUI into the google javascript
QString javascript
= QString("geocoder.getLocations(\"" + street
->text
() + " " + street2
->text
() + ", " + city
->text
() + ", " + state
->currentText
() + ", " + zip
->text
() + "\", showAddress);");
webView->page()->currentFrame()->evaluateJavaScript(javascript);
}
//examine the map now that we sent an address to it
examineWait();
}
//single shot timer, to wait to examine the map (lets google catch up)
//tune the time variable based on your load times
void DialogMap::examineWait() {
QTimer::singleShot(1000,
this,
SLOT(examinePage
()));
}
void DialogMap::examinePage() {
QVariant lat
= webView
->page
()->currentFrame
()->evaluateJavaScript
(javascript
);
QVariant lng
= webView
->page
()->currentFrame
()->evaluateJavaScript
(javascript
);
examineTries--;
//if we got results, then we can stop trying:
if ((examineSuccess == false)&&(lat.toString() != "")&&(lng.toString() != "")) {
//reset our tries for the next time the user clicks/requests an address
examineSuccess = true;
examineTries = 10;
//since we grabbed the lat and lng, reset them to "", so we
//don't pull them again out of the page until user selects a new
//point
javascript
= QString("Lat = \"\";\nLng = \"\";");
webView->page()->currentFrame()->evaluateJavaScript(javascript);
////////////////////////////////////////////
//do something with your lat and lng vars here
////////////////////////////////////////////
//if we found nothing then try again until examinetries has 0 left
} else {
//if we have tries left, then we try again:
if ((examineSuccess == false)&&(examineTries > 0)) {
examineWait();
}
if ((examineSuccess == false)&&(examineTries == 0)) {
if (!examinetimedout) {
//qDebug()<< "Timed out!";
qApp
->tr
("Loading Google Map timed out!\n\nPlease click \"Calculate >>\" to try again."),
QMessageBox::Ok,
examinetimedout = true;
}
}
}
}
class DialogMap: public QDialog, private Ui::DialogMap {
Q_OBJECT
public:
//functions:
DialogMap(QWidget *parent = 0);
//variables:
private slots:
void calcMap();
void examineWait();
void examinePage();
private:
bool examineSuccess;
int examineTries;
bool examinetimedout;
};
//setup interface of main window:
DialogMap::DialogMap(QWidget *parent) : QDialog(parent) {
examineSuccess = false;
examinetimedout = false;
examineTries = 10;
setupUi(this);
//if we click on the button in the GUI we call that slot:
connect(calcButton, SIGNAL(clicked()), this, SLOT(calcMap()));
//this is to make sure the program examines the script after the map is first loaded:
connect(webView, SIGNAL(loadFinished(bool)), this, SLOT(examineWait()));
///////////////////////////////////////////////////////////////////
//also can have other slots here, such as a "clicked" signal connecting to examinewait
//for when the map is clicked by the user
//or any other signal that might trigger you to examine the lat/lng in the map
//////////////////////////////////////////////////////////////////////
//load the view with the map page:
webView->setUrl(QUrl("http://domain/map.html"));
}
//make the map process the GUi vars we pass to the javascript:
void DialogMap::calcMap() {
//reset success to false because we have not examined it yet:
examineSuccess = false;
examinetimedout = false;
examineTries = 10;
//input the vars from our GUI into the google javascript
QString javascript = QString("geocoder.getLocations(\"" + street->text() + " " + street2->text() + ", " + city->text() + ", " + state->currentText() + ", " + zip->text() + "\", showAddress);");
webView->page()->currentFrame()->evaluateJavaScript(javascript);
}
//examine the map now that we sent an address to it
examineWait();
}
//single shot timer, to wait to examine the map (lets google catch up)
//tune the time variable based on your load times
void DialogMap::examineWait() {
QTimer::singleShot(1000, this, SLOT(examinePage()));
}
void DialogMap::examinePage() {
QString javascript = QString("getlat()");
QVariant lat = webView->page()->currentFrame()->evaluateJavaScript(javascript);
javascript = QString("getlng()");
QVariant lng = webView->page()->currentFrame()->evaluateJavaScript(javascript);
examineTries--;
//if we got results, then we can stop trying:
if ((examineSuccess == false)&&(lat.toString() != "")&&(lng.toString() != "")) {
//reset our tries for the next time the user clicks/requests an address
examineSuccess = true;
examineTries = 10;
//since we grabbed the lat and lng, reset them to "", so we
//don't pull them again out of the page until user selects a new
//point
javascript = QString("Lat = \"\";\nLng = \"\";");
webView->page()->currentFrame()->evaluateJavaScript(javascript);
////////////////////////////////////////////
//do something with your lat and lng vars here
////////////////////////////////////////////
//if we found nothing then try again until examinetries has 0 left
} else {
//if we have tries left, then we try again:
if ((examineSuccess == false)&&(examineTries > 0)) {
examineWait();
}
if ((examineSuccess == false)&&(examineTries == 0)) {
if (!examinetimedout) {
//qDebug()<< "Timed out!";
QMessageBox::critical(0, qApp->tr("Map Timed Out!"),
qApp->tr("Loading Google Map timed out!\n\nPlease click \"Calculate >>\" to try again."), QMessageBox::Ok,
QMessageBox::NoButton);
examinetimedout = true;
}
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks