2011-11-24 7 views
1

私はAndroid開発でphonegapを使用しています。私はそのPoCを書いたが、なぜそれがプロファイル変数の緯度を変えないのか分からない。実際Android onDeviceReady関数のPhoneGap

geoCode.setLocation(); 

alert(profile.latitude); 

実行ここに私のコードです。

document.addEventListener("deviceready", onDeviceReady, false); 

var profile = { 
    name: "", 
    id: "red", 
    latitude: "xx", 
    longtitude: "", 
    setCoordinates: function (latit, longt) { 
     this.latitude = latit; 
     this.longtitude = longt; 
    } 
}; 

var geoCode = { 
    onSuccess: function (position) { 
     profile.latitude = position.coords.latitude; 
    }, 

    onError: function (error) { 
    }, 

    setLocation : function() { 
     navigator.geolocation.getCurrentPosition(this.onSuccess, this.onError); 
    } 
}; 

// Wait for PhoneGap to load 
// 

function onDeviceReady() { 

    geoCode.setLocation(); 
    //alert("2"); 
    alert(profile.latitude); 
}; 

navigator.geolocation.getCurrentPosition()の呼び出しは非同期呼び出しであるため、事前

答えて

1

navigator.geolocation.getCurrentPositionは非同期関数です。あなたは次のようなことをする必要があります:

var geoCode = { 


setLocation : function (callback) { 

    onSuccess: function (position) { 
     callback(position.coords.latitude); 
    }, 

    onError: function (error) { 
    }, 
    navigator.geolocation.getCurrentPosition(onSuccess, onError); 
} 

}; 

// Wait for PhoneGap to load 
// 

function onDeviceReady() { 

    geoCode.setLocation(function(latitude) { 
     alert(latitude); 
    }); 
}; 
0

でのおかげでかなり単にそれがあります。これにより、プログラムの実行が継続され、アラートが表示されます。アラートが表示された後、geoCodeクラスのonSuccess呼び出しをprofile.latitude値の更新と呼びます。

関連する問題