Results 1 to 15 of 15

Thread: evaluateJavaScript function can be used for loading maps in qt widgets?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default evaluateJavaScript function can be used for loading maps in qt widgets?

    I tried the c++ code from this example: http://wiki.forum.nokia.com/index.ph..._With_QWebView
    It compiled but didn't load the map or the marker. I am NOT trying to load the map on some website. In fact I am trying to load it in a Qt widget.
    This code connect(this,SIGNAL(reloadMap()), this,SLOT(loadCoordinates())); from that example never got called.

    Then when I tried to call the function loadCoordinates myself at the end of the constructor, nothing happened, no maps no markers.

    Is this problem related to evaluateJavaScript function?
    I am missing some point here, I think. I need help.

  2. #2
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    Slightly modified code:
    Qt Code:
    1. #include "hello.h"
    2. #include <QApplication>
    3. #include <QWebView>
    4. #include <QUrl>
    5. #include <QNetworkReply>
    6. #include <QWebFrame>
    7.  
    8. using namespace std;
    9. #include <iostream>
    10.  
    11. QGoogleMapView::QGoogleMapView(QWidget *parent) : QWebView(parent), pendingRequests(0)
    12. {
    13. manager = new QNetworkAccessManager(this);
    14. geoCode ("");
    15. connect (manager, SIGNAL (finished(QNetworkReply*)), this, SLOT (replyFinished(QNetworkReply*)));
    16.  
    17. connect (this, SIGNAL (reloadMap()), this, SLOT (loadCoordinates()));
    18. }
    19.  
    20.  
    21. void QGoogleMapView::geoCode(const QString& address)
    22. {
    23. cout << "\ngeocode\n";
    24. clearCoordinates();
    25.  
    26. QString requestStr( tr("http://maps.google.com/maps/geo?q=%1&output=%2&key=%3")
    27. .arg("citibank, singapore")
    28. .arg("csv"));
    29.  
    30. manager->get (QNetworkRequest (requestStr));
    31. }
    32.  
    33. void QGoogleMapView::replyFinished (QNetworkReply *reply)
    34. {
    35. cout << "\nreplyfinished\n";
    36.  
    37. QString replyStr (reply->readAll());
    38. QStringList coordinateStrList = replyStr.split(",");
    39.  
    40. if (coordinateStrList.size() == 4)
    41. {
    42. QPointF coordinate( coordinateStrList[2].toFloat(),coordinateStrList[3].toFloat() );
    43. coordinates << coordinate;
    44. }
    45.  
    46. emit reloadMap();
    47. }
    48.  
    49. void QGoogleMapView::loadCoordinates()
    50. {
    51. cout << "\nloadcoor";
    52.  
    53. QStringList scriptStr;
    54. scriptStr
    55. << "var map = new GMap2(document.getElementById(\"map\"));"
    56. << "var bounds = new GLatLngBounds;"
    57. << "var markers = [];"
    58. << "map.setCenter( new GLatLng(0,0),1 );";
    59.  
    60. int num=-1;
    61. foreach( QPointF point, coordinates ) {
    62. scriptStr << QString("markers[%1] = new GMarker(new GLatLng(%2, %3));")
    63. .arg(++num)
    64. .arg(point.x())
    65. .arg(point.y());
    66. }
    67.  
    68. scriptStr
    69. << "for( var i=0; i<markers.length; ++i ) {"
    70. << " bounds.extend(markers[i].getPoint());"
    71. << " map.addOverlay(markers[i]);"
    72. << "}"
    73. << "map.setCenter(bounds.getCenter());";
    74.  
    75. this->page()->mainFrame()->evaluateJavaScript(scriptStr.join ("\n"));
    76. }
    77.  
    78. void QGoogleMapView::clearCoordinates()
    79. {
    80. coordinates.clear();
    81. }
    82.  
    83. int main(int argc, char *argv[])
    84. {
    85. QApplication a(argc, argv);
    86.  
    87. QGoogleMapView w;
    88. w.show();
    89.  
    90. return a.exec();
    91. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    Is Google maps API script included in the webpage?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:

    TheIndependentAquarius (27th May 2011)

  5. #4
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    What does that mean? I don't have a webpage or a phone, I am doing this on my computer on a qt widget.
    Please help.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    Quote Originally Posted by Anisha Kaul View Post
    What does that mean? I don't have a webpage or a phone, I am doing this on my computer on a qt widget.
    Please help.
    As far as I understand you are using QWebView which works on webpages, aka objects of the QWebPage class. If you want to evaluate some piece of javascript containing references to "GMap2" objects, the definition of "GMap2" has to be available. Did you make it available or do you expect QWebView to somehow magically guess what "GMap2" is?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    TheIndependentAquarius (27th May 2011)

  8. #6
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    Quote Originally Posted by wysota View Post
    If you want to evaluate some piece of javascript containing references to "GMap2" objects, the definition of "GMap2" has to be available. Did you make it available or do you expect QWebView to somehow magically guess what "GMap2" is?
    I didn't know anything about GMap2, I'll google and see how that is to be incorporated in c++ code.

    Thanks for being around, why don't I get the mail notifications, I don't understand!

    Anyway, There is a flaw in the way I am passing the Javascript code to the evaluatejavascript func. of QWebView.

    Using Google maps's API, when I click a pushButton attached to the slot holding the below code

    this->page()->mainFrame()->evaluateJavaScript (QString ("Open(%1,2)").arg ( point.x ()).arg (point.y ()) );

    the map pertaining to the location in question gets displayed.

    Now if I want to add a marker to a particular coordinate, I do:

    this->page()->mainFrame()->evaluateJavaScript (QString ("addMarker (%1, %2)").arg (point.x ()).arg (point.y ()) );

    This code doesn't execute. Any ideas?

    Besides this, what is the way to add a user defined function to evaluateJavaScript for evaluation?

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    It doesn't execute or nothing happens when it executes?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #8
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    sorry, I meant to say, I have written:
    Qt Code:
    1. this->page()->mainFrame()->evaluateJavaScript (QString("addMarker(%1,%2)").arg(point.x()).arg(point.y()) );
    2.  
    3. this->page()->mainFrame()->evaluateJavaScript (QString("Open(%1,%2)").arg(point.x()).arg(point.y()) );
    To copy to clipboard, switch view to plain text mode 
    The second statement shows the effect on the map on the widget, OTOH, the first one doesn't do anything.

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    Shouldn't you execute "addMarker" on the map object?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #10
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    I do understand what you are saying, but I do not understand how all this works. I mean addMarker is a google javascript API and can be used only through "evaluateJavaScript" function. I don't know the syntax. I searched google too much, but in vain.

    Can you give a small example?

    See this: http://code.google.com/apis/maps/doc...Marker_Manager

    But I don't know how to embedd this in c++ through evaluateJavaScript?

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    Then maybe stop searching and read a couple of tutorials for the API.

    Here is an example of using markers using the old API: http://code.google.com/intl/pl/apis/...er-simple.html
    And here is a general Marker documentation for the old API: http://code.google.com/intl/pl/apis/...s.html#Markers

    But I would suggest that you start here, with the new API: http://code.google.com/intl/pl/apis/...pt/basics.html

    And do the hello world tutorial: http://code.google.com/intl/pl/apis/...tml#HelloWorld
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    TheIndependentAquarius (27th May 2011)

  15. #12
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    Thanks, but I already had all those links, and now I have found the way to get them into c++ source.

    Any Javascript function which has to be called from function evaluateJavaScript is supposed to defined in an html file read by the C++ source as shown below:


    Now instead of creating a new add marker function, I have added its code in the Open function defined below:
    Now a marker gets shown on the map.

    Qt Code:
    1. var map;
    2.  
    3. function initialize()
    4. {
    5. if (GBrowserIsCompatible())
    6. {
    7. map = new GMap2(document.getElementById("map"));
    8. map.setCenter( new GLatLng(0,0),1 );
    9. }
    10. }
    11.  
    12. function Open (x,y)
    13. {
    14. map.setCenter (new GLatLng(x,y), 13);
    15.  
    16. var point = new GLatLng (x,y);
    17. map.addOverlay (new GMarker(point));
    18. }
    To copy to clipboard, switch view to plain text mode 

    Thanks again for being around.

  16. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    Well... "having links" and understanding the concepts described in documents behind those links are two different things.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #14
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    Those links show how to write the code in Javascript.

    My question to you was how to write a userdefined javascript function which can be called in the Qt's api. See post 6.

    now I know that these functions have to be defined in an html file which is to be included in the source code.

  18. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: evaluateJavaScript function can be used for loading maps in qt widgets?

    Quote Originally Posted by Anisha Kaul View Post
    My question to you was how to write a userdefined javascript function which can be called in the Qt's api. See post 6.
    Why would you think it would differ in any way?

    now I know that these functions have to be defined in an html file which is to be included in the source code.
    No, that's not true.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. pyqt: uic.loadUiTyoe() multiple ui widgets loading
    By qtpyqt in forum Qt Programming
    Replies: 0
    Last Post: 26th May 2010, 15:49
  2. Replies: 18
    Last Post: 21st May 2010, 15:08
  3. Delay in loading qt widgets
    By pinakis in forum Qt Programming
    Replies: 1
    Last Post: 3rd June 2009, 06:56
  4. Name mangling dll function loading
    By TheKedge in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 19:40
  5. Calling Recursivly loading function in Run() method of QThread
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 14:42

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.