I know some of you are getting tired of me already, but bare with me, I think this is the last hickup
I have an changed handler that’s supposed to fire when the location is changed.
However, I have not changed the location in the emulator, and it fires like every 2 seconds or something…
Here’s the console.log of Date.now()
changed 1494352058914
changed 1494352059984
changed 1494352061070
changed 1494352062125
changed 1494352063137
changed 1494352064135
The code is:
<App>
<JavaScript>
var Observable = require("FuseJS/Observable");
var GeoLocation = require("FuseJS/GeoLocation");
var hikes = [{"Title":"This is a hike", "Description":"It's a very good hike","LocalPhoto":"Assets/screen2.png"}]
var trails = [{"Title":"This is a trail", "Description":"It's a very good trail","LocalPhoto":"Assets/screen2.png", "Locations":[{"lat":"41.996075","lon":"21.431812"}]}]
// Immediate
var Tours = Observable();
var timeoutLocation = Observable("");
var isDialogShowing = Observable(false);
function distance(lat1, lon1, lat2, lon2)
{
var R = 6371; // km
var dLat = toRad(lat2-lat1);
var dLon = toRad(lon2-lon1);
var lat1 = toRad(lat1);
var lat2 = toRad(lat2);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d * 1000;
}
// Converts numeric degrees to radians
function toRad(Value)
{
return Value * Math.PI / 180;
}
function startContinuousListener() {
var intervalMs = 1000;
var desiredAccuracyInMeters = 10;
GeoLocation.startListening(intervalMs, desiredAccuracyInMeters);
}
function stopContinuousListener() {
GeoLocation.stopListening();
}
startContinuousListener()
var observedLocation = GeoLocation.observe("changed");
var mapLocation = observedLocation;
GeoLocation.on("error", function(err){console.log(err)});
GeoLocation.on("changed", function(loc){
console.log(JSON.stringify(observedLocation))
if (observedLocation.value){
var dist = distance(41.995876, 21.431812, observedLocation.value.latitude, observedLocation.value.longitude)
if ((dist < 50)){
// Display modal
isDialogShowing.value = true;
console.log('check 1')
console.log(isDialogShowing);
}
}
});
var mapLocation = observedLocation;
var continuousLocation = observedLocation.map(JSON.stringify);
function testToggleBool() {
isDialogShowing.value = ! isDialogShowing.value;
}
module.exports = {
timeoutLocation: continuousLocation,
mapLocation: mapLocation,
hikes: hikes,
trails: trails,
isDialogShowing: isDialogShowing,
testToggleBool: testToggleBool
};
</JavaScript>
Why does this happen? It would be nice if this fires ONLY when the location is changed.