我使用此代码从javascript获取getData:
const QVariant myvar = this->page()->mainFrame()->
evaluateJavaScript("measureDistance("+QString::fromStdString(prev->marker_id)+","+QString::fromStdString(stop->marker_id+");"));
bool ok;
const int myint = myvar.toInt(&ok);
if (!ok)
qWarning() << "Error getting int from JS";
qDebug() << myint;
getMapTimeDistance(location, QString::fromStdString(prev->getName()), stop, 10);
在javascript中,最后一行是:
function measureDistance(m1, m2) {
var wp = new Array();
var lstart = new google.maps.LatLng(markers[m1].position);
var lend = new google.maps.LatLng(markers[m2].position);
var request = {
origin : lstart,
destination : lend,
waypoints : [],
optimizeWaypoints: true,
travelMode : google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status== google.maps.DirectionsStatus.OK) {
response.routes[0].legs[0].duration.text;
} else {
document.getElementById('locationD').value = "drawRouteBad";
}
});
}
你知道为什么这段代码对我不起作用吗?我尝试过不同类型的组合,但没有任何帮助。可能我不知道javasscripts值是什么类型。也许我应该以不同的方式将它传递给qt?
答案 0 :(得分:0)
response.routes[0].legs[0].duration.value;
来自Google Maps API v3 Directions Service(按your last question。该服务是异步的,您无法通过调用返回任何内容,您有在回调函数中使用您需要的任何数据(当它从服务器返回并且可用时)。