Results 1 to 1 of 1

Thread: Problem with Google Maps in QT Widget

  1. #1
    Join Date
    Dec 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with Google Maps in QT Widget

    Hi,

    I have currently compiled a QT app to display Google Maps inside QT widget but have issues with dragging across the map with the mouse as well as accessing the drawing tools displayed on the map. It seems like there is no interaction between the mouse buttons and the actual map inside the widget. The zoom button and the navigation control works but not the drawing tools (markers, circle, polygon etc.) and when I drag the mouse across, nothing happens.

    Below is what the GUI looks like.

    Screenshot at 2011-12-20 12_04_56.jpg

    And here's the code.

    ________
    GMap.cpp
    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3.  
    4. #include "QGMap.h"
    5.  
    6. //------------------------------------------HTML FILE-------------------------------------------------
    7.  
    8. #define MAP_HTML "<html> "\
    9. "<head> "\
    10. "<script type=\"text/javascript\" "\
    11. "src=\"http://maps.googleapis.com/maps/api/js?sensor=false&libraries=drawing\"></script> "\
    12. "</script> "\
    13. "<script type=\"text/javascript\"> "\
    14. "function initialize(lat, lng) { "\
    15. "var myOptions = { "\
    16. "zoom: 16, "\
    17. "center: new google.maps.LatLng(lat, lng), "\
    18. "mapTypeId: google.maps.MapTypeId.ROADMAP, "\
    19. "disableDefaultUI: false, "\
    20. "draggable: true, "\
    21. "panControl: true, "\
    22. "scaleControl: true, "\
    23. "zoomControl: true, "\
    24. "zoomControlOptions: { "\
    25. "style: google.maps.ZoomControlStyle.LARGE, "\
    26. "position: google.maps.ControlPosition.LEFT_CENTER "\
    27. "}, "\
    28. "}; "\
    29. "var map = new google.maps.Map(document.getElementById(\"map_canvas\"), "\
    30. "myOptions); "\
    31. "var drawingManager = new google.maps.drawing.DrawingManager({ "\
    32. "drawingMode: google.maps.drawing.OverlayType.MARKER, "\
    33. "drawingControl: true, "\
    34. "drawingControlOptions: { "\
    35. "position: google.maps.ControlPosition.TOP_CENTER, "\
    36. "drawingModes: [google.maps.drawing.OverlayType.MARKER, "\
    37. "google.maps.drawing.OverlayType.CIRCLE, "\
    38. "google.maps.drawing.OverlayType.POLYGON, "\
    39. "google.maps.drawing.OverlayType.POLYLINE, "\
    40. "google.maps.drawing.OverlayType.RECTANGLE] "\
    41. "}, "\
    42. "circleOptions: { "\
    43. "fillColor: '#FF000', "\
    44. "fillOpacity: 1, "\
    45. "strokeWeight: 5, "\
    46. "clickable: false, "\
    47. "zIndex: 1, "\
    48. "editable: true "\
    49. "} "\
    50. "}); "\
    51. "drawingManager.setMap(map); "\
    52. "google.maps.event.addListener(map, 'click', function(event) { "\
    53. "placeMarker(event.latLng); "\
    54. "}) "\
    55. "} "\
    56. "</script> "\
    57. "</head> "\
    58. "<body style=\"margin:0px; padding:0px;\"> "\
    59. "<body onload=\"initialize()\"> "\
    60. "<div id=\"map_canvas\" style=\"width:100%; height:100%\"></div> "\
    61. "</body> "\
    62. "</html> "
    63.  
    64. //----------------------------------------END OF HTML FILE-------------------------------------------
    65.  
    66.  
    67.  
    68. GMaps::GMaps(QWidget *parent = 0): QWebView(parent)
    69. {
    70. zoomPage = new QWebPage(this);
    71. connect(zoomPage, SIGNAL(repaintRequested(QRect)), SLOT(update()));
    72.  
    73. QString content = MAP_HTML;
    74. QWebFrame *frame = page()->mainFrame();
    75. frame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
    76. frame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
    77. frame->setHtml(content);
    78. QTimer::singleShot(1000, this, SLOT(triggerLoading()));
    79. }
    80.  
    81.  
    82. void GMaps::setCenter(qreal latitude, qreal longitude)
    83. {
    84. QString code = "map.set_center(new google.maps.LatLng(%1, %2));";
    85. QWebFrame *frame = page()->mainFrame();
    86. frame->evaluateJavaScript(code.arg(latitude).arg(longitude));
    87. frame = zoomPage->mainFrame();
    88. frame->evaluateJavaScript(code.arg(latitude).arg(longitude));
    89. }
    90.  
    91. void GMaps::triggerLoading()
    92. {
    93. QString code = "initialize(-27.5171, 152.8914)";
    94. QWebFrame *frame = page()->mainFrame();
    95. frame->evaluateJavaScript(code);
    96. frame = zoomPage->mainFrame();
    97. frame->evaluateJavaScript(code);
    98. }
    99.  
    100. QGMap::QGMap(): QMainWindow(0)
    101. {
    102. map = new GMaps(this);
    103. setCentralWidget(map);
    104. }
    To copy to clipboard, switch view to plain text mode 
    ______
    GMap.h

    Qt Code:
    1. #ifndef QGMap_H
    2. #define QGMap_H
    3.  
    4. #include <QtWebKit>
    5. #include <QMainWindow>
    6.  
    7. class GMaps: public QWebView
    8. {
    9. Q_OBJECT
    10.  
    11. private:
    12. QWebPage *zoomPage;
    13. // QPointF CurrentPoint;
    14. // QPoint pressPos;
    15. // bool pressed;
    16.  
    17. public:
    18. GMaps(QWidget *parent);
    19. void setCenter(qreal latitude, qreal longitude);
    20.  
    21. private slots:
    22. void triggerLoading();
    23. };
    24.  
    25. class QGMap: public QMainWindow
    26. {
    27. Q_OBJECT
    28.  
    29. public:
    30. GMaps *map;
    31. QGMap();
    32. };
    33.  
    34.  
    35. #endif // QGMap_H
    To copy to clipboard, switch view to plain text mode 
    ________
    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "QGMap.h"
    3.  
    4. int main(int argc, char **argv)
    5. {
    6.  
    7. QApplication app(argc, argv);
    8.  
    9. QGMap MainWindow;
    10. MainWindow.setWindowTitle("Maps (powered by Google)");
    11. MainWindow.resize(800, 600);
    12. MainWindow.show();
    13.  
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    Thanks.
    Last edited by Yifan; 20th December 2011 at 06:10.

Similar Threads

  1. QWebView Problem Drag&Drop Markers in Google Maps
    By MightTower in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2011, 00:19
  2. Replies: 0
    Last Post: 14th April 2011, 20:54
  3. Mercator for google static maps
    By daarsh in forum Qt Programming
    Replies: 6
    Last Post: 29th September 2010, 12:43
  4. Google Maps API and Qt 4 desktop app integration
    By udit in forum Qt Programming
    Replies: 6
    Last Post: 4th September 2009, 20:03
  5. Map is not displayed from maps.google.com
    By abhilashajha in forum Qt Programming
    Replies: 2
    Last Post: 11th June 2009, 15:19

Tags for this Thread

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.