Results 1 to 11 of 11

Thread: WebKit - accessing javascript results + googleMap

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 4 Times in 4 Posts

    Default Re: WebKit - accessing javascript results + googleMap

    Qt Code:
    1. class DialogMap: public QDialog, private Ui::DialogMap {
    2.  
    3. Q_OBJECT
    4.  
    5. public:
    6. //functions:
    7. DialogMap(QWidget *parent = 0);
    8.  
    9. //variables:
    10.  
    11. private slots:
    12. void calcMap();
    13. void examineWait();
    14. void examinePage();
    15.  
    16. private:
    17. bool examineSuccess;
    18. int examineTries;
    19. bool examinetimedout;
    20.  
    21. };
    22.  
    23. //setup interface of main window:
    24. DialogMap::DialogMap(QWidget *parent) : QDialog(parent) {
    25.  
    26.  
    27. examineSuccess = false;
    28. examinetimedout = false;
    29. examineTries = 10;
    30.  
    31.  
    32. setupUi(this);
    33.  
    34.  
    35. //if we click on the button in the GUI we call that slot:
    36.  
    37. connect(calcButton, SIGNAL(clicked()), this, SLOT(calcMap()));
    38.  
    39. //this is to make sure the program examines the script after the map is first loaded:
    40.  
    41. connect(webView, SIGNAL(loadFinished(bool)), this, SLOT(examineWait()));
    42. ///////////////////////////////////////////////////////////////////
    43. //also can have other slots here, such as a "clicked" signal connecting to examinewait
    44. //for when the map is clicked by the user
    45. //or any other signal that might trigger you to examine the lat/lng in the map
    46. //////////////////////////////////////////////////////////////////////
    47.  
    48.  
    49.  
    50. //load the view with the map page:
    51. webView->setUrl(QUrl("http://domain/map.html"));
    52.  
    53.  
    54.  
    55. }
    56.  
    57.  
    58. //make the map process the GUi vars we pass to the javascript:
    59. void DialogMap::calcMap() {
    60. //reset success to false because we have not examined it yet:
    61. examineSuccess = false;
    62. examinetimedout = false;
    63. examineTries = 10;
    64.  
    65. //input the vars from our GUI into the google javascript
    66. QString javascript = QString("geocoder.getLocations(\"" + street->text() + " " + street2->text() + ", " + city->text() + ", " + state->currentText() + ", " + zip->text() + "\", showAddress);");
    67. webView->page()->currentFrame()->evaluateJavaScript(javascript);
    68. }
    69.  
    70. //examine the map now that we sent an address to it
    71. examineWait();
    72.  
    73. }
    74.  
    75.  
    76.  
    77. //single shot timer, to wait to examine the map (lets google catch up)
    78. //tune the time variable based on your load times
    79. void DialogMap::examineWait() {
    80. QTimer::singleShot(1000, this, SLOT(examinePage()));
    81. }
    82.  
    83.  
    84. void DialogMap::examinePage() {
    85.  
    86. QString javascript = QString("getlat()");
    87. QVariant lat = webView->page()->currentFrame()->evaluateJavaScript(javascript);
    88.  
    89. javascript = QString("getlng()");
    90. QVariant lng = webView->page()->currentFrame()->evaluateJavaScript(javascript);
    91.  
    92. examineTries--;
    93.  
    94. //if we got results, then we can stop trying:
    95. if ((examineSuccess == false)&&(lat.toString() != "")&&(lng.toString() != "")) {
    96.  
    97. //reset our tries for the next time the user clicks/requests an address
    98.  
    99. examineSuccess = true;
    100. examineTries = 10;
    101.  
    102. //since we grabbed the lat and lng, reset them to "", so we
    103. //don't pull them again out of the page until user selects a new
    104. //point
    105. javascript = QString("Lat = \"\";\nLng = \"\";");
    106. webView->page()->currentFrame()->evaluateJavaScript(javascript);
    107.  
    108. ////////////////////////////////////////////
    109. //do something with your lat and lng vars here
    110. ////////////////////////////////////////////
    111.  
    112. //if we found nothing then try again until examinetries has 0 left
    113. } else {
    114. //if we have tries left, then we try again:
    115. if ((examineSuccess == false)&&(examineTries > 0)) {
    116. examineWait();
    117. }
    118. if ((examineSuccess == false)&&(examineTries == 0)) {
    119. if (!examinetimedout) {
    120. //qDebug()<< "Timed out!";
    121. QMessageBox::critical(0, qApp->tr("Map Timed Out!"),
    122.  
    123. qApp->tr("Loading Google Map timed out!\n\nPlease click \"Calculate >>\" to try again."), QMessageBox::Ok,
    124.  
    125. QMessageBox::NoButton);
    126. examinetimedout = true;
    127. }
    128. }
    129. }
    130.  
    131. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2008
    Posts
    276
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Re: WebKit - accessing javascript results + googleMap

    Did you try your code?
    G

  3. #3
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    48
    Thanked 4 Times in 4 Posts

    Default Re: WebKit - accessing javascript results + googleMap

    Yes and it works. The example I posted is a good start but not totally complete. You would need to make the UI file in designer, add the code in main to exec the dialog, and of course some code to do something with the data you gathered.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.