2013-11-20 74 views
0

我在回撥地獄,我真的不知道我會如何擺脫它。 我試過閱讀其他涉及這個主題的主題,但我仍然沒有把握這個概念。回調地獄,地理定位 - JavaScript

我正在使用Google Maps API v3,並試圖返回用戶當前位置,以便我可以繼續將其傳遞給其他方法。

我已成功通過使用jQuery .done()out of context,它引發警告,但我設法得到我的座標對象。

如果我調用userPosition,在返回變量userPosition之前如何讓該方法等待findUser方法?

編輯:上傳錯誤的代碼,並在編輯後,縮進糟透了。 :)

userPosition: function(position) 
      { 
       if (typeof position === 'undefined') 
       { 
         //Call on findUser, when ready callback it self to set variables 
       } 
       else 
       { 
        //var userPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
        //return userPosition; 
       } 
      }, 

findUser: function() 
       { 
        var self = this; 
        if (navigator.geolocation) 
        { 
         navigator.geolocation.getCurrentPosition(
          function(position) { // If successful 
           self.userPosition(position); 
          }, 
          function(e) { // If something went wrong 
           var errors = { 
            1: 'Permission denied', 
            2: 'Position unavailable', 
            3: 'Request timeout' 
           }; 
           alert("Error: " + errors[e.code]); 
          }, 
          { enableHighAccuracy: true, timeout: this.timeoutVal, maximumAge: 0 } 
         ); 
        } 
        else 
        { 
         // Set flag 
        } 
       }, 
+0

如果位置未定義,請嘗試訪問其「coords」屬性? 'userPosition'返回局部變量,但是調用'userPosition'不使用返回值?你確定這段代碼做你認爲它的作用嗎? –

+0

因爲它是張貼在上面的userPosition方法,如果我打電話findUser。我想要做的不是調用findUser,而是調用userPosition,如果userPosition(position)未定義,則調用方法findUser並在回調函數中設置var userPosition並返回它。 這樣我可以調用另一種方法,比如showUser,它根據座標繪製地圖上的標記。 – dojs

+0

我不明白它是如何工作的。如果'position'未定義,則嘗試訪問'position.coords'應該拋出ReferenceError。編輯:和代碼已經改變;) –

回答

0
var isUserLocationSet = false; 
userPosition: function(position){ 

       if(!isUserLocationSet) 
       { 
        settimeout(userPosition, 200); 
        return; 
       } 
       if (typeof position === 'undefined') 
       { 
        var userPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
        return userPosition; 
       } 
       console.log(typeof position); 
      }, 

findUser: function(){ 
      //set isUserLocationSet to true on completion in this method 
      var self = this; 
      if (navigator.geolocation) 
      { 
       navigator.geolocation.getCurrentPosition(
        function(position) { // If successful 
         self.userPosition(position); 
        }, 
        function(e) { // If something went wrong 
         var errors = { 
          1: 'Permission denied', 
          2: 'Position unavailable', 
          3: 'Request timeout' 
         }; 
         alert("Error: " + errors[e.code]); 
        }, 
        { enableHighAccuracy: true, timeout: this.timeoutVal, maximumAge: 0 } 
       ); 
      } 
      else 
      { 
       // Set flag 
      } 
     } 

在這裏,你會檢查該標誌採用反覆setTimeout和直到標誌設置將保持調用該函數。

+0

是的,可以工作,但我寧願依靠純回調比設置setTimeout。無論如何,謝謝你。 – dojs

2

以下是我在jsfiddle中完成的工作。

var thing = { 

    findUser: function (callback) { 
    var _this = this; 

    // use the getPos method to get the geolocation 
    this.getPos(function (position) { 
     var lat, lng, userPos; 
     lat = position.coords.latitude; 
     lng = position.coords.longitude; 

     // add the new lat lng to the userPosition variable 
     _this.userPosition = new google.maps.LatLng(lat, lng); 

     // invoke the callback 
     callback(); 
    }); 
    return this; 
    }, 

    getPos: function (callback) { 
    var geo = navigator.geolocation; 
    if (geo) { 

     // get the geolocation and pass it back to findUser 
     geo.getCurrentPosition(callback); 
    } 
    return this; 
    } 
} 

// if userPosition doesn't exist then grab the geolocation 
if (!thing.userPosition) { 
    thing.findUser(function() { 
    console.log(thing.userPosition); 
    }); 
} else { 
    console.log(thing.userPosition); 
}