1

デバイスから 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。これを修正するにはどうすればよいですか? 何か案は?

ありがとう

4

1 に答える 1

2

私の質問に対する答えは、 onSuccess および onError 関数を呼び出すときにこれを使用しないことです。正しい呼び出しは次のとおりです。

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

これは Kerry Shotts によって指摘されました。

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

于 2015-07-01T18:35:47.623 に答える