PDA

View Full Version : Reference Error: google is not defined



pkarthick
3rd March 2011, 12:30
Hi All,
I am trying to execute javascript through Qt script.
My javascript contains code for google maps API.
when i ran it through Qt script, it gives an Error like "Reference
Error: google is not defined".

I think the problem is the

<script type="text/javascript" src="http://maps.google.com/maps/api/js?
sensor=false">
</script>
line from the html file. If I use the <> brackets, the java script
engine won't compile it, and if I omit the line, the program gets the
"google not found" error...

Please anyone help me how to call the URL inside <script> tag in
javascript which is been executed by Qt script?

NOTE:
Here, I have included JS and Qt script file.
JS file:

function calcRoute() {
var directionsService = new google.maps.DirectionsService(); //
getting error here."google not defined"
var start = "chennai";
var end = "bangalore";
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(resp, status) {
if (status == google.maps.DirectionsStatus.OK) {
return resp;
}

});
}

QtScript file:

#include <QApplication>
#include <QtScript>
#include <QScriptEngine>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QScriptEngine engine;
QScriptValue global = engine.globalObject();

QString fileName1 = "direction.js";
QFile scriptFile1(fileName1);
if(!scriptFile1.open(QIODevice::ReadOnly))
{
qDebug() << "error";
}
QTextStream stream1(&scriptFile1);
QString contents1 = stream1.readAll();
scriptFile1.close();
QScriptValue result1 = engine.evaluate(contents1, fileName1);
if(engine.hasUncaughtException()) {
int line = engine.uncaughtExceptionLineNumber();
qDebug() << "uncaught exception at line" << line << ":" <<
result1.toString();
}
QScriptValue val1 = global.property("calcRoute");
QScriptValueList args1;
QScriptValue final_res1 = val1.call(QScriptValue(), args1); //
calling JS function calcRoute with no args
qDebug() << "The result is: " << final_res1.toString();
return app.exec();
}

wysota
6th March 2011, 09:31
You need to import the remote javascript file into your script. You'll probably have to download it first.

Added after 20 minutes:

Also read this: http://labs.qt.nokia.com/2011/03/04/faking-a-web-browser-environment-in-qtscript/