我正在使用从设备读取 gps 坐标的 HTML/JavaScript 应用程序。我遇到的问题是我收到以下错误:
Uncaught TypeError: Failed to execute 'getCurrentPosition' on 'Geolocation': The callback provided as parameter 1 is not a function.
这是JavaScript:
var app = {
position: null,
// Application Constructor
initialize: function() {
alert("app.initialize invoked");
console.log("Initializing google map");
$(document).ready(this.onDeviceReady);
},
// Execute on success - when calling this function invoke with this. prefixed
onSuccess: function()
{
alert("app.onSuccess invoked");
var lat = geolocation.coords.latitude;
var lng = geolocation.coords.longitude;
localStorage.setItem("Latitude", lat);
localStorage.setItem("Longitude", lng);
alert("lat: " + lat + "long: " + lng);
},
// Execute on failure - when calling this function invoke with this. prefixed
onError: function()
{
alert("Code: " + error.message);
},
// Execute on deviceReady - when calling this function invoke with this. prefixed
onDeviceReady: function()
{
alert("app.onDeviceReady invoked");
var options = { enableHighAccuracy: true };
alert("app.onDeviceReady invoked +2");
//<------------------- vv Does not work vvv -------------------
navigator.geolocation.watchPosition(this.onSuccess, this.onError, options);
alert("app.onDeviceReady invoked +3");
},
// Attach google map to div
showGoogleMap: function()
{
}
};
function onLoad() {
alert("onLoad invoked");
app.initialize();
}
所以它不喜欢我注册回调函数的方式——this.onSuccess。我应该如何解决这个问题?有任何想法吗?
谢谢