我正在使用名为leaflet和leaflet-routing的javscript库来显示地图并计算从起点到终点的路线。到现在为止还挺好。为此,我使用WebView表单JavaFX来加载包含所需javascript的html文件。 这是需要这样做的javascript:
function initMap()
{
map = L.map('map');
}
function addLayers()
{
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
}
function calculateRoutingExample()
{
control = L.Routing.control({
waypoints: [
L.latLng(currentPosition.latitud, currentPosition.longitud),
L.latLng(destination.latitud, destination.longitud)
],
routeWhileDragging: false,
reverseWaypoints: true,
showAlternatives: true,
altLineOptions: {
styles: [
{color: 'black', opacity: 0.15, weight: 9},
{color: 'white', opacity: 0.8, weight: 6},
{color: 'blue', opacity: 0.5, weight: 2}
]
}
}).addTo(map);
L.Routing.errorControl(control).addTo(map);
//Esconder las indicaciones de texto sobre el mapa
control.hide();
}
正如您所看到的,我使用了一个存储大量有用信息的变量调用控件。我想要的是能够从java访问路由点。为此,在javascript上我可以得到一个包含所需信息的Json对象。使用以下调用,我得到一个JSON格式的String。 我们的想法是在java中获取此字符串,以便能够转换为JSON对象,然后在应用程序的其他部分使用它。
JSON.stringify(control._routes[0]);
在网络浏览器javascript调试控制台上使用此方法获得的值:
"{"name":"Carrer del Taulat, Carrer de Venero","coordinates":[{"lat":41.399859,"lng":2.206204},{"lat":41.399594,"lng":2.205881},{"lat":41.399546,"lng":2.205821},{"lat":41.399158,"lng":2.205341},{"lat":41.398923,"lng":2.205051},{"lat":41.398597,"lng":2.204657},{"lat":41.398972,"lng":2.204181},{"lat":41.399085,"lng":2.204137},{"lat":41.399127,"lng":2.20405},{"lat":41.399128,"lng":2.203947},{"lat":41.399351,"lng":2.203647},{"lat":41.399772,"lng":2.203082},{"lat":41.399837,"lng":2.203096},{"lat":41.3999,"lng":2.203073},{"lat":41.399958,"lng":2.203004},{"lat":41.399983,"lng":2.202891},{"lat":41.400024,"lng":2.20277},{"lat":41.400668,"lng":2.201952},{"lat":41.400731,"lng":2.20196},{"lat":41.400825,"lng":2.201939},{"lat":41.401106,"lng":2.202296},{"lat":41.402006,"lng":2.20112},{"lat":41.40175,"lng":2.20079},{"lat":41.401697,"lng":2.200763},{"lat":41.40159,"lng":2.200775},{"lat":41.401529,"lng":2.200777},{"lat":41.401495,"lng":2.200734},{"lat":41.401447,"lng":2.200652},{"lat":41.401086,"lng":2.201118}],"instructions":[{"type":"Straight","distance":191,"time":24,"road":"Carrer del Taulat","direction":"SW","index":0},{"type":"Right","distance":58,"time":11,"road":"Rambla del Poblenou","direction":"NW","index":5},{"type":"Straight","distance":13,"time":2,"road":"","direction":"N","index":6},{"type":"SlightLeft","distance":17,"time":2,"road":"","direction":"NW","index":7},{"type":"Right","distance":102,"time":20,"road":"Rambla del Poblenou","direction":"NW","index":9},{"type":"Straight","distance":33,"time":4,"road":"","direction":"N","index":11},{"type":"SlightRight","distance":110,"time":21,"road":"Rambla del Poblenou","direction":"W","index":15},{"type":"Straight","distance":18,"time":2,"road":"","direction":"N","index":17},{"type":"Straight","distance":43,"time":6,"road":"Carrer de Llull","direction":"NE","index":19},{"type":"Left","distance":140,"time":17,"road":"carrer de Venero","direction":"NW","index":20},{"type":"Straight","distance":40,"time":5,"road":"Carrer de Pujades","direction":"SW","index":21},{"type":"SlightLeft","distance":39,"time":6,"road":"","direction":"S","index":22},{"type":"Left","distance":56,"time":10,"road":"Rambla del Poblenou","direction":"SE","index":27},{"type":"DestinationReached","distance":0,"time":0,"road":"","direction":"N","index":28}],"summary":{"totalDistance":860,"totalTime":129},"inputWaypoints":[{"options":{"allowUTurn":false},"latLng":{"lat":41.4,"lng":2.206},"_initHooksCalled":true},{"options":{"allowUTurn":false},"latLng":{"lat":41.401,"lng":2.201},"_initHooksCalled":true}],"waypoints":[{"options":{"allowUTurn":false},"latLng":{"lat":41.399859,"lng":2.206204},"_initHooksCalled":true},{"options":{"allowUTurn":false},"latLng":{"lat":41.401086,"lng":2.201118},"_initHooksCalled":true}],"waypointIndices":[0,28],"routesIndex":0}"
从Java可以使用此调用调用javascript函数:
WebEngine engine = webView.getEngine();
String JSONRoute = engine.executeScript("getJSONRoute()");
在javascript方面:
function getJSONRoute()
{
return JSON.stringify(control._routes[0]);
}
理论上,javascript上的函数将String返回给Java,稍后我将能够使用它。我试过这个例子字符串,比如“嗨,我是消息”,它有效。
但是当我这样做时,我收到了这个错误:
Error getting JSON route : TypeError: undefined is not an object (evaluating 'control._routes[0]')
如何在我的Java APP中的javascript中从getJSONRoute()返回此JSON?
谢谢你!