dimanche 5 avril 2015

Logic on managing users GPS location

My Xcode iPhone app continuously queries a list of GPS coordinates stored on Parse.com. The list of points (coordinates) can grow as more points are added to it daily by other users.


When the user is navigating using the app, the app will alert the user via an audio cue that they have entered the radius of one of the points as stored in Parse.


The app continuously checks the users current location against the list of points from Parse and when it finds that the user is in the radius of any point, the audio cue is played. This is all working perfect.


However, I only want the audio cue to play once while the user is in the points radius. Currently my audio cue will play over and over again while the user is in a points radius.


Below is the logic I have for determining whether to play a sound. As mentioned above, the logic to determine whether you are in the radius of a point is working perfect. Just the audio cue playing over and over again is the issue here.


The variable hasPlayedSound is a BOOL that is initialised to FALSE in the viewDidLoad() method.



if (distanceInMeters <= 50)
{
if (!(hasPlayedSound))
{
[self playSound:@"Untitled"];
hasPlayedSound = true;
}
}
else
{
hasPlayedSound = false;
}


The logic above does not work because: If the user enters a points radius for the first time, the distanceInMeters is less than 50 and the hasPlayedSound is false. So the audio cue plays and hasPlayedSound is set to TRUE.


The next iteration of the loop to check user location vs point radius however checks the next point in the list (not the point just checked) on Parse and that point might not be in the users radius. So hasPlayedSound is set to false in the ELSE loop. Then it iterates over all the other points and when it gets back to the original point in which radius you are, because of the other points that weren't in your radius, the hasPlayedSound is set back to FALSE and the sound is played again.


How can I make the warning play only once for each point that you enter? It will have to play for each point you enter, but only once?


Aucun commentaire:

Enregistrer un commentaire