I am trying to fetch the current location each time the user clicks a button.
function checkConnection() {
// check if there is connection and call the getcurrent position function
navigator.geolocation.getCurrentPosition(onLocSuccess, onLocError);
}
function onLocSuccess(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
myPosition = new google.maps.LatLng(latitude, longitude);
//..........
//..........
// do something more
//.....
}
On the browser this works fine. But on the device (testing on iphone5), after 2-3 times of requesting, this piece of code does not fetch the location. It freezes, resulting in the app to freeze.
But interestingly, if i place an alert inside onLocSuccess it works fine !!!
function onLocSuccess(position) {
alert('this alert solves the issue');
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
myPosition = new google.maps.LatLng(latitude, longitude);
//..........
//..........
// do something more
//.....
}
What could be wrong ?
Your code seems correct. I guess, there is some other issue behind the app freezing. Still you can try and check this once,
getCurrentLocation : function() {
var me = this;
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position) {
me.latitude = position.coords.latitude
me.longitude = position.coords.longitude;
// User's location'
me.position = new google.maps.LatLng(me.latitude, me.longitude);
});
} else {
Ext.Msg.alert("Sorry, browser does not support geolocation!");
}
}
Finally used settimeout . This works !
function onLocSuccess(position) {
setTimeout(function () {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
myPosition = new google.maps.LatLng(latitude, longitude);
//..........
//..........
// do something more
//.....
}, 50);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.