2012-04-12 83 views
0

我正在使用Phonegap的移動應用程序,它將使用地理位置。之前我使用了Geolocation,但是這次我認爲爲它創建一個新的對象包裝會更好,因爲大部分功能都是基於這個功能的,並且不希望以混亂的代碼結束。Javascript Objects/Geolocation API

解決方案可能是直截了當的,但它擊敗了我,因爲我從來沒有在JS中使用對象做過任何非常先進的事情。下面是目前的代碼(刪除不需要的部分):

function Geolocation(maximumAge, accurate) { 
    this.settings = { 
      'maximumAge': maximumAge, 
      'accurate': accurate 
    } 
} 

Geolocation.prototype = { 
    position: { 
     latitude: null, 
     longitude: null, 
     altitude: null, 
     accuracy: null, 
     altitudeAccuracy: null, 
     heading: null, 
     speed: null 
    }, 
    lastCheck: null, 
    watchId: null, 
    onSuccess: function(position) { 
     this.position.latitude = position.coords.latitude; 
     this.position.longitude = position.coords.longitude; 
     this.position.altitude = position.coords.altitude; 
     this.position.accuracy = position.coords.accuracy; 
     this.position.altitudeAccuracy = position.coords.altitudeAccuracy; 
     this.position.heading = position.coords.heading; 
     this.position.speed = position.coords.speed; 
     this.lastCheck = new Date(position.timestamp); 
    }, 
    onError: function(error) { 
     console.log(error.code+' '+error.message); 
    }, 
    getCoordinates: function() { 
     if (this.watchId == null) { 
      this.watchId = navigator.geolocation.watchPosition(this.onSuccess, this.onError, { maximumAge: this.settings.maximumAge, enableHighAccuracy: this.settings.accurate}); 
     } 
     return { 
      latitude: this.position.latitude, 
      longitude: this.position.longitude 
     }; 
    } 
}; 

正如你也許已經注意到,當我打電話getCoordinates(),回調成功的功能是(我猜)出對象的範圍,因此不知道「這個」是什麼......任何想法如何解決這個問題或者什麼是正確的實現?

謝謝你的時間!

後來編輯:我知道我需要修改該函數,只有在找到位置後才返回座標。目前還不知道如何做到這一點,但如果您有任何提示,那將非常棒!

回答

-1

OK,我想通了,一部分被來自有關綁定範圍爲回調函數這個其他線程的幫助:JavaScript Callback Scope

要回到我使用一個回調函數座標。包括下面的代碼,我將其標記爲社區維基 - 但如果任何人有任何建議,請繼續前進並製作它們。一切似乎都工作正常,但也許我沒有在這裏做一些事情。

function Geolocation(maximumAge, accurate) { 
    this.settings = { 
      'maximumAge': maximumAge, 
      'accurate': accurate 
    } 
} 

Geolocation.prototype = { 
    position: { 
     latitude: null, 
     longitude: null, 
     altitude: null, 
     accuracy: null, 
     altitudeAccuracy: null, 
     heading: null, 
     speed: null 
    }, 
    lastCheck: null, 
    callback: null, 
    watchId: null, 
    onSuccess: function(position) { 
     this.position.latitude = position.coords.latitude; 
     this.position.longitude = position.coords.longitude; 
     this.position.altitude = position.coords.altitude; 
     this.position.accuracy = position.coords.accuracy; 
     this.position.altitudeAccuracy = position.coords.altitudeAccuracy; 
     this.position.heading = position.coords.heading; 
     this.position.speed = position.coords.speed; 
     this.lastCheck = new Date(position.timestamp); 

     var pos = { 
      latitude: this.position.latitude, 
      longitude: this.position.longitude 
     }; 
       // call callback with position and accuracy parameters 
     this.callback(pos, this.position.accuracy); 
    }, 
    onError: function(error) { 
     console.log(error.code+' '+error.message); 
    }, 
    getCoordinates: function(callback) { 
       // Helper function to bind scope to callback function as seen at https://stackoverflow.com/questions/183214/javascript-callback-scope 
     function bind(scope, fn) { 
      return function() { 
       fn.apply(scope, arguments); 
      }; 
     } 
       // Assign the callback function to the local member 
     this.callback = callback; 
       // watchPosition is a method that gets called each time the position changes. Making sure it's only getting called once (watchId can be used to stop the function when necessary 
     if (this.watchId == null) { 
         // notice usage of the bind function here 
      this.watchId = navigator.geolocation.watchPosition(bind(this, this.onSuccess), bind(this, this.onError), { maximumAge: this.settings.maximumAge, enableHighAccuracy: this.settings.accurate}); 
     } 
    } 
}; 

用例:

var geo = new Geolocation(3000, true); 
geo.getCoordinates(createMap); 

function createMap(position, accuracy) { 
    // Your code here 
}