Tracking User Location

You must Login before you can answer or comment on any questions.

var trackuser = function newLocationTracker() {
    Ti.API.info("Inside Tracking user");
    central.phonePosition(global_map, 0.05, 0.05, function(curr_lat, curr_long, zoom_lat, zoom_long) {
        Ti.API.info("Tracking user");
    });
};
Central.prototype.locationTracker = function(parent_obj) {
    global_map = parent_obj.map;
    if (parent_obj.drawMapState == "on") {
        if (Ti.App.tracking == "on") Titanium.Geolocation.removeEventListener('location', trackuser);
        Titanium.Geolocation.addEventListener('location', trackuser);
        Ti.App.tracking = "on";
    }
    else {
        Titanium.Geolocation.removeEventListener('location', trackuser);
        Ti.App.tracking = "off";
    }
}
I have this code which tracks the user position, but I have noticed that sometimes the 'addEventListener' does not call the matching function. 'global_map' to a map object which I change from time to time depending on which map needs to set the user position.

Can anyone see a reason why the function may not fire when the 'addEventListner' code is called.

1 Answer

Hmm... My best guess would be a race condition between these two lines:

Titanium.Geolocation.removeEventListener('location', trackuser);
Titanium.Geolocation.addEventListener('location', trackuser);
I assume what you're doing there is trying to keep the event from being bound twice. Maybe a better way would be to use another state for Ti.App.tracking? Eg. off/on/active -- and then if it's set to "on" you can add the event listener, if it's "active" you can assume the event listener is added already, and if it's "off" you remove the listener.

Hope that helps!

Your Answer

Think you can help? Login to answer this question!