简体   繁体   English

JavaScript地理位置错误-回调函数错误

[英]JavaScript Geolocation Error - callback function error

I am working HTML/JavaScript app that reads gps coordinates from the device. 我正在使用从设备读取gps坐标的HTML / JavaScript应用。 The issue I am having is that I get the following error: 我遇到的问题是出现以下错误:

Uncaught TypeError: Failed to execute 'getCurrentPosition' on 'Geolocation': The callback provided as parameter 1 is not a function.

Here is the JavaScript: 这是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();
}

So it doesn't like the way I am registering the callback function - this.onSuccess. 因此,它不喜欢我注册回调函数-this.onSuccess的方式。 How should I fix this? 我该如何解决? Any ideas? 有任何想法吗?

Thanks 谢谢

The answer to my question is to NOT use this when calling the onSuccess and onError function. 我的问题的答案是在调用onSuccess和onError函数时不要使用函数。 The correct invocation is: 正确的调用是:

  position = navigator.geolocation.getCurrentPosition(app.onSuccess, app.onError, options);

This was pointed out by Kerry Shotts: 克里·肖茨指出:

https://groups.google.com/forum/#!topic/phonegap/TIjA3TPpQt0 https://groups.google.com/forum/#!topic/phonegap/TIjA3TPpQt0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM