Reference Error: google is not defined
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
Code:
<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:
Code:
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:
Code:
#include <QApplication>
#include <QtScript>
#include <QScriptEngine>
int main(int argc, char **argv)
{
QScriptEngine engine;
QScriptValue global = engine.globalObject();
QString fileName1
= "direction.js";
QFile scriptFile1
(fileName1
);
{
qDebug() << "error";
}
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();
}
Re: Reference Error: google is not defined
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/...t-in-qtscript/