I have a simple locations map and I want to make my app beep when the user is approaching a location that is listed in a remote file
the listed locations are on my server named locations.txt
how can i check locations.txt every 1 minute to see if the user is within 300m of a location??
The standard answer to this question is Shape-Based Regions as described in the Location Awareness Guide. Generally, shape-based regions is the way to go if you have a limited number of regions. But, given that you want a lot of regions, you might have to “roll your own”:
Turn on a location service and monitor your location. See the Location Awareness Programming Guide. If you use standard location service, make sure to set a
desiredAccuracythat is as low as possible to achieve the functional need (e.g.kCLLocationAccuracyHundredMeters).Once you’ve successfully received the first
didUpdateLocations, if you really want to check every minute, you could create a timer at that point. If the purpose of that timer is to just check the users’ location, then the timer is really not needed and you can just wait for occurrences of thedidUpdateLocations.You can iterate through your array of locations to monitor (I’d convert them to
CLLocationobjects) and simply usedistanceFromLocation.A couple of observations, though:
You suggest that you want to check
locations.txtevery minute to see if user is within 300m of location. I can imagine two reasons why you might have proposed that solution:Did server’s
locations.txtchange? If this is the problem you’re trying to solve, a better solution would be push notifications (a.k.a. “remote notifications”) and you want to make sure the client has access to the latest information. The process of constantly re-retrieving the file is very expensive (in terms of bandwidth, battery, computationally); orDid the user move? If you’re concerned about whether the user may have moved, the right solution is not to check every minute, but rather wait for the [
CLLocationManagerDelegate] instance methoddidUpdateLocationsto be called. If you want to avoid too many redundant checks, you can always keep track of whether the last request took place less than a minute ago or not, and then only check again if it was more than a minute ago. But that’s very different than checking every minute whether you need to or not.You’ve suggested using a text file. You might want to contemplate using a JSON file (or XML file), which is a better mechanism for retrieving data from a server.
For example, if you have a text file in JSON format, you can parse the results in another single line of code (
JSONObjectWithData). To illustrate, let me show you what a JSON file might look like (where the square brackets designate an array, and the curly braces designate a dictionary, this is therefore an array of dictionaries):Then your app can retrieve the results incredibly easily with two lines:
So, you’ll need to start location services:
You’ll then have a routine for checking the current location:
And this will be called when the user’s location changes:
The implementation might look like this test project on GitHub. This is a barebones implementation, but it gives you an idea of the tools you have at hand, namely retrieving your
locations.jsonfile and comparing that to the location retrieved by the device.