我正在使用W3C Geolocation API來檢索GWT Web應用程序中的位置信息。地理位置API是在回調位置位置後立即調用回調函數的方式構建的。從GWT的內部函數調用Java方法JSNI
我目前的測試代碼如下:
public native void getCoordinates() /*-{
function onPositionUpdate(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var alt = position.coords.altitude;
// not OK,- doesn't get invoked, and function execution stops at this point
[email protected]::testMethod()();
alert("Coordinates retrieved: " + lat + ";" + lng + ";" + alt);
}
function onPositionUpdateFailed(error) {
alert("Some error");
}
if(navigator.geolocation) {
// OK when invoked somewhere here
[email protected]::testMethod()();
navigator.geolocation.getCurrentPosition(onPositionUpdate, onPositionUpdateFailed);
} else {
alert("Geolocation is not available");
}
}-*/;
我試圖證明testMethod
沒有得到來自內部方法onPositionUpdate
調用的方法。甚至更多:之後沒有顯示警報(它在刪除testMethod調用時顯示),但是在日誌中沒有發現警告或錯誤。
我的問題是:如何從內部函數引用此方法?我覺得this.
不是內部函數的正確參考。如果Java類無法正確引用,那麼可能的解決方法是什麼?請注意,我不能通過任何其他功能onPositionUpdate()
,因爲它是一個回調函數。
可以通過我們com.test.GpsPanel 作爲com.test.GpsPanel取代例如@ com.test.GpsPanel :: TestMethod的()(); – 2013-11-28 08:15:53