首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaScript地理定位误差-回调函数误差

JavaScript地理定位误差-回调函数误差
EN

Stack Overflow用户
提问于 2015-06-26 21:57:04
回答 1查看 6.4K关注 0票数 1

我正在使用HTML/JavaScript应用程序,从设备中读取gps坐标。我遇到的问题是,我得到了以下错误:

代码语言:javascript
复制
Uncaught TypeError: Failed to execute 'getCurrentPosition' on 'Geolocation': The callback provided as parameter 1 is not a function.

下面是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();
}

所以它不喜欢我注册回调函数- this.onSuccess的方式。我该怎么解决这个问题?有什么想法吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-01 18:35:47

我问题的答案是在调用onSuccess和onError函数时不要使用它。正确的调用是:

代码语言:javascript
复制
  position = navigator.geolocation.getCurrentPosition(app.onSuccess, app.onError, options);

克里·索特指出了这一点:

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31082763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档