Reputation: 3
I am building an app in Swift and I have a quick question.
When the user enters a certain location, (e.g. shop) I want it to display a list of items saved in Firebase Database in a UITableView (can vary in different locations).
I've done a bit of location stuff in Swift before but nothing like this.
Help would be much appreciated!
Thanks so much,
Jake.
{
"venues" : {
"CrokePark" : {
"beverages" : {
"drink1" : "Coca-Cola",
"drink2" : "Fanta",
"drink3" : "Dr. Pepper"
},
"orders" : {
"Fanta" : {
"addedByUser" : "[email protected]",
"completed" : false,
"name" : "Aaron"
},
"Coca-Cola" : {
"addedByUser" : "[email protected]",
"completed" : false,
"name" : "Jake"
}
}
}
}
}
Upvotes: 0
Views: 305
Reputation: 3867
In order to achieve what you're looking for, you'd generally want to have the exact location i.e. (latitude, longitude), of the cafe stored in your database. So your venue structure would look as such
"venues" : {
"CrokePark" : {
"latitude": 37.335556, // this is a double
"longitude": -122.009167 // this is a double
"beverages" : {
"drink1" : "Coca-Cola",
"drink2" : "Fanta",
"drink3" : "Dr. Pepper"
}
With the location you have to worry of retrieval at this point. Generally speaking, you'd want to notify the user when they are a certain radius; r, from the venue. Two methods come in mind
Lousy Battery Draining Method
Always get an update of their current location, query the database to see if their current location is within r. If yes, notify them then stop getting their location and querying Firebase.
This method is the easiest to implement but you sincerely have got to have a serious issue with your users should you opt for it.
Slightly Improved
let currentLocation = CLLocation2D()
let possibleDestinations = []
timer == expired
roll back to Step 1
currentLocation equal or close to any address in possibleDestinations
timer.invalidate()
and present the info of the venue from possibleDestinations
Obviously the second method is more complex than the first one but its more user-friendly; in terms of battery consumption, and efficient.
Side Note:
I made the latitude
and longitude
nodes store Double
respectively so as you can simply pass them into the CLLocationDegrees
constructor. More info
Since you seem to be relatively new to the Core Location API, I'd recommend you create a side project first and learn how to perform geo-fencing before integrating it into your application. Current Location Tutorial. Geofencing Tutorial.
Upvotes: 1