最简单的方法是使GeographicLib
全球可用:
结束时地理库.js文件、更改
window.GeographicLib = geo;
to
this.GeographicLib = geo;
然后你就可以使用:
主要.qml:
import QtQuick 2.12
import QtQuick.Window 2.12
import "geographiclib.js" as ThenItWillBeAvailableGlobally
Window {
visible: true
width: 640
height: 480
Component.onCompleted: {
var Geodesic = GeographicLib.Geodesic,
DMS = GeographicLib.DMS,
geod = Geodesic.WGS84;
var r = geod.Inverse(23, 22, 44, 29);
console.log("distance is: ", JSON.stringify(r))
}
}
导致:
qml: distance is: {"lat1":23,"lat2":44,"lon1":22,"lon2":29,"a12":21.754466225665134,"s12":2416081.7576307985,"azi1":13.736139413215236,"azi2":17.669059640534535}
如果你不想改变地理库.js文件,您可以使用以下命令添加全局窗口对象:
窗口.js:
this.window = this;
然后使用:
import QtQuick 2.12
import QtQuick.Window 2.12
import "window.js" as ThenWindowWillBeAvailableGlobally
import "geographiclib.js" as ThenGeographicLibWillBeAvailableGlobally
Window {
visible: true
width: 640
height: 480
Component.onCompleted: {
var Geodesic = GeographicLib.Geodesic,
DMS = GeographicLib.DMS,
geod = Geodesic.WGS84;
var r = geod.Inverse(23, 22, 44, 29);
console.log("distance is: ", JSON.stringify(r))
}
}
如果您不想添加任何全局变量但您愿意编辑地理库.js文件,那么你可以将第 68 行移动到文件的顶部:
var GeographicLib = {};
/*
* Geodesic routines from GeographicLib translated to JavaScript. See
* https://geographiclib.sourceforge.io/html/js/
并在文件末尾更改
} else {
/******** otherwise just pollute our global namespace ********/
window.GeographicLib = geo;
}
});
to
} else if (typeof window === 'object') {
/******** otherwise just pollute our global namespace ********/
window.GeographicLib = geo;
}
});
然后你的main.qml会工作得很好。