Reputation: 871
how can I get latitude and longitude outside created object? I've made new object of Ext.util.Geolocation, updated it, and now I'm trying to get Latitude and Longitude values outside the object, but it shows me 'null'. Code:
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
Thanks in advance.
Upvotes: 0
Views: 2421
Reputation: 10217
This is just a timing issue - obtaining the location is not synchronous, therefore you can not access it outside the locationupdate
callback function, at least until it has been initialized.
You should do whatever you need to do inside that callback (e.g. call another function passing in the latitude if it needs)...
...
listeners: {
locationupdate: function(geo) {
yourCallbackFunction(geo.getLatitude());
},
...
If you really need to use it outside that callback, then in the worst case you can do something like:
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);
Upvotes: 0
Reputation: 12949
It's probably because it takes some time to get your location.
You should try to get the latitude and the longitude within the locationupdate
callback function.
If you want to access it outside, just make sure that geo exists
if (geo) { alert(geo.getLatitude()); }
Hope this helps
Upvotes: 1