Sencha Touch 2 Ext.util.Geolocation
问题描述:
如何获得创建对象之外的经度和纬度? 我已经做了Ext.util.Geolocation的新对象,更新了它,现在我试图让纬度和经度值在对象之外,但是它显示我'null'。 代码:Sencha Touch 2 Ext.util.Geolocation
var geo = Ext.create('Ext.util.Geolocation', {
autoUpdate: false,
listeners: {
locationupdate: function(geo) {
//alert('New latitude: ' + geo.getLatitude());
},
locationerror: function(geo, bTimeout, bPermissionDenied, bLocationUnavailable, message) {
if(bTimeout){
alert('Timeout occurred.');
} else {
alert('Error occurred.');
}
}
}
});
geo.updateLocation();
//geo.fireEvent('locationupdate');
alert(geo.getLatitude()); // it shows null
在此先感谢。
答
这只是一个时间问题 - 获得位置是不同步的,所以你不能在locationupdate
回调函数之外访问它,至少在它被初始化之前。
你应该做你需要一个回调里面做(如呼叫传递,如果它需要纬度其他功能)...
...
listeners: {
locationupdate: function(geo) {
yourCallbackFunction(geo.getLatitude());
},
...
如果你真的需要使用该回调之外,则在最坏的情况下,你可以这样做:
var latitude; //will store latitude when ready..
var geo = Ext.create('Ext.util.Geolocation', {
autoUpdate: false,
listeners: {
locationupdate: function(geo) {
latitude = geo.getLatitude();
}
}
});
geo.updateLocation();
//waits until latitude exists then does something..
var untilLatThere = setInterval(function(){
if(latitude) {
alert(latitude); //alerts a valid value..
clearInterval(untilLatThere);
}
}, 100);
尝试使用geo.updateLocation(); – Multitut 2012-10-11 17:35:02