PDA

View Full Version : evaluateJavaScript function can be used for loading maps in qt widgets?



TheIndependentAquarius
26th May 2011, 09:03
I tried the c++ code from this example: http://wiki.forum.nokia.com/index.php/Google_Maps_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.

TheIndependentAquarius
26th May 2011, 11:04
Slightly modified code:


#include "hello.h"
#include <QApplication>
#include <QWebView>
#include <QUrl>
#include <QNetworkReply>
#include <QWebFrame>

using namespace std;
#include <iostream>

QGoogleMapView::QGoogleMapView(QWidget *parent) : QWebView(parent), pendingRequests(0)
{
manager = new QNetworkAccessManager(this);
geoCode ("");
connect (manager, SIGNAL (finished(QNetworkReply*)), this, SLOT (replyFinished(QNetworkReply*)));

connect (this, SIGNAL (reloadMap()), this, SLOT (loadCoordinates()));
}


void QGoogleMapView::geoCode(const QString& address)
{
cout << "\ngeocode\n";
clearCoordinates();

QString requestStr( tr("http://maps.google.com/maps/geo?q=%1&output=%2&key=%3")
.arg("citibank, singapore")
.arg("csv"));

manager->get (QNetworkRequest (requestStr));
}

void QGoogleMapView::replyFinished (QNetworkReply *reply)
{
cout << "\nreplyfinished\n";

QString replyStr (reply->readAll());
QStringList coordinateStrList = replyStr.split(",");

if (coordinateStrList.size() == 4)
{
QPointF coordinate( coordinateStrList[2].toFloat(),coordinateStrList[3].toFloat() );
coordinates << coordinate;
}

emit reloadMap();
}

void QGoogleMapView::loadCoordinates()
{
cout << "\nloadcoor";

QStringList scriptStr;
scriptStr
<< "var map = new GMap2(document.getElementById(\"map\"));"
<< "var bounds = new GLatLngBounds;"
<< "var markers = [];"
<< "map.setCenter( new GLatLng(0,0),1 );";

int num=-1;
foreach( QPointF point, coordinates ) {
scriptStr << QString("markers[%1] = new GMarker(new GLatLng(%2, %3));")
.arg(++num)
.arg(point.x())
.arg(point.y());
}

scriptStr
<< "for( var i=0; i<markers.length; ++i ) {"
<< " bounds.extend(markers[i].getPoint());"
<< " map.addOverlay(markers[i]);"
<< "}"
<< "map.setCenter(bounds.getCenter());";

this->page()->mainFrame()->evaluateJavaScript(scriptStr.join ("\n"));
}

void QGoogleMapView::clearCoordinates()
{
coordinates.clear();
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QGoogleMapView w;
w.show();

return a.exec();
}

wysota
26th May 2011, 13:32
Is Google maps API script included in the webpage?

TheIndependentAquarius
27th May 2011, 06:07
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.

wysota
27th May 2011, 07:50
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?

TheIndependentAquarius
27th May 2011, 08:31
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?

wysota
27th May 2011, 08:44
It doesn't execute or nothing happens when it executes?

TheIndependentAquarius
27th May 2011, 08:48
sorry, I meant to say, I have written:


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

this->page()->mainFrame()->evaluateJavaScript (QString("Open(%1,%2)").arg(point.x()).arg(point.y()) );
The second statement shows the effect on the map on the widget, OTOH, the first one doesn't do anything.

wysota
27th May 2011, 08:51
Shouldn't you execute "addMarker" on the map object?

TheIndependentAquarius
27th May 2011, 09:05
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/documentation/javascript/v2/overlays.html#Marker_Manager

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

wysota
27th May 2011, 10:01
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/maps/documentation/javascript/v2/examples/marker-simple.html
And here is a general Marker documentation for the old API: http://code.google.com/intl/pl/apis/maps/documentation/javascript/v2/overlays.html#Markers

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

And do the hello world tutorial: http://code.google.com/intl/pl/apis/maps/documentation/javascript/tutorial.html#HelloWorld

TheIndependentAquarius
27th May 2011, 10:19
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.



var map;

function initialize()
{
if (GBrowserIsCompatible())
{
map = new GMap2(document.getElementById("map"));
map.setCenter( new GLatLng(0,0),1 );
}
}

function Open (x,y)
{
map.setCenter (new GLatLng(x,y), 13);

var point = new GLatLng (x,y);
map.addOverlay (new GMarker(point));
}

Thanks again for being around.

wysota
27th May 2011, 10:23
Well... "having links" and understanding the concepts described in documents behind those links are two different things.

TheIndependentAquarius
27th May 2011, 10:31
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.

wysota
27th May 2011, 11:29
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.