I am using Geofire and Cordova Geolocation in my Ionic app like this :
//Catch the position of the user
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var posOptions = {timeout: 10000, enableHighAccuracy: true};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
var lat = position.coords.latitude
var long = position.coords.longitude
var userId = firebase.auth().currentUser.uid;
// Insert geofire location of userId
var firebaseRef = firebase.database().ref('/geolocation/');
var geoFire = new GeoFire(firebaseRef);
geoFire.set(userId, [lat, long]).then(function() {
}, function(error) {
});
// end geofire
firebase.database().ref('accounts/' + userId).update({
lat: lat,
long: long,
})
}, function(err) {
});
var watchOptions = {timeout : 3000, enableHighAccuracy: true};
var watch = $cordovaGeolocation.watchPosition(watchOptions);
watch.then(
null,
function(err) {
},
function(position) {
var lat = position.coords.latitude
var long = position.coords.longitude
var userId = firebase.auth().currentUser.uid;
firebase.database().ref('accounts/' + userId).update({
lat: lat,
long: long,
})
}
);
watch.clearWatch();
}
I try to have the location being automatically caught by the app when the user moves. For example, the user moves a few meters and that updates it with this code.
However I can not find a way to have it that way. Am I doing something wrong ?