Structuring real time location data request from firebase using swift
up vote
1
down vote
favorite
So I'm trying to retrieve the location of a "paired user" (i.e. not the current user but a contact of the current user) and monitor the distance travelled and distance relative to the currentUser. When using the code below I am only able to retrieve a list with multiple entries of the same location rather than consistently getting only the new location update of the 'paired user'.
func retrieveLocations() {
let locationDB = Database.database().reference().child("Locations").child("nameOfUser")
locationDB.queryLimited(toLast: 1).observe(.childAdded)
(snapshot) in
let snapshotValue = snapshot.value as! Dictionary<String,Any>
let lats = snapshotValue["Lat"]
let longs = snapshotValue["Lon"]
let timeOf = snapshotValue["Time"]
let sender = snapshotValue["Sender"]
let otherUsersLocations = CLLocation(latitude: CLLocationDegrees(truncating: lats! as! NSNumber), longitude: CLLocationDegrees(truncating: longs! as! NSNumber), timestamp: timeOf as! Int)
print("Latitude: (otherUsersLocations.coordinate.latitude), Longitude: (otherUsersLocations.coordinate.longitude), Time: (timeOf), Sender: (sender)")
I later call the function in the same func that I use to update the current users location. The code below is used to update the current users location to firebase.Firebase Database screenshot
let locationDictionary = [
"Sender": Auth.auth().currentUser?.email,
"Lat": latitude,
"Lon": longitude,
"Speed": speed,
"Time": ServerValue.timestamp()
] as [String : Any]
locationDB.childByAutoId().setValue(locationDictionary)
(error, reference) in
if error != nil
print(error!)
else
retrieveLocations()
I have tried creating a new "LocationModel" object but had troubles feeding the dictionary of LocationModel type with the retrieved data from firebase.
So my objective is to retrieve an array/dictionary of the location data for the paired user from "now" and subsequently measure the distance to the currentUser.
I've been sitting at this for a while so I really appreciate any suggestions.
Thanks a lot!
Pat
swift firebase firebase-realtime-database location data-retrieval
add a comment |
up vote
1
down vote
favorite
So I'm trying to retrieve the location of a "paired user" (i.e. not the current user but a contact of the current user) and monitor the distance travelled and distance relative to the currentUser. When using the code below I am only able to retrieve a list with multiple entries of the same location rather than consistently getting only the new location update of the 'paired user'.
func retrieveLocations() {
let locationDB = Database.database().reference().child("Locations").child("nameOfUser")
locationDB.queryLimited(toLast: 1).observe(.childAdded)
(snapshot) in
let snapshotValue = snapshot.value as! Dictionary<String,Any>
let lats = snapshotValue["Lat"]
let longs = snapshotValue["Lon"]
let timeOf = snapshotValue["Time"]
let sender = snapshotValue["Sender"]
let otherUsersLocations = CLLocation(latitude: CLLocationDegrees(truncating: lats! as! NSNumber), longitude: CLLocationDegrees(truncating: longs! as! NSNumber), timestamp: timeOf as! Int)
print("Latitude: (otherUsersLocations.coordinate.latitude), Longitude: (otherUsersLocations.coordinate.longitude), Time: (timeOf), Sender: (sender)")
I later call the function in the same func that I use to update the current users location. The code below is used to update the current users location to firebase.Firebase Database screenshot
let locationDictionary = [
"Sender": Auth.auth().currentUser?.email,
"Lat": latitude,
"Lon": longitude,
"Speed": speed,
"Time": ServerValue.timestamp()
] as [String : Any]
locationDB.childByAutoId().setValue(locationDictionary)
(error, reference) in
if error != nil
print(error!)
else
retrieveLocations()
I have tried creating a new "LocationModel" object but had troubles feeding the dictionary of LocationModel type with the retrieved data from firebase.
So my objective is to retrieve an array/dictionary of the location data for the paired user from "now" and subsequently measure the distance to the currentUser.
I've been sitting at this for a while so I really appreciate any suggestions.
Thanks a lot!
Pat
swift firebase firebase-realtime-database location data-retrieval
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
So I'm trying to retrieve the location of a "paired user" (i.e. not the current user but a contact of the current user) and monitor the distance travelled and distance relative to the currentUser. When using the code below I am only able to retrieve a list with multiple entries of the same location rather than consistently getting only the new location update of the 'paired user'.
func retrieveLocations() {
let locationDB = Database.database().reference().child("Locations").child("nameOfUser")
locationDB.queryLimited(toLast: 1).observe(.childAdded)
(snapshot) in
let snapshotValue = snapshot.value as! Dictionary<String,Any>
let lats = snapshotValue["Lat"]
let longs = snapshotValue["Lon"]
let timeOf = snapshotValue["Time"]
let sender = snapshotValue["Sender"]
let otherUsersLocations = CLLocation(latitude: CLLocationDegrees(truncating: lats! as! NSNumber), longitude: CLLocationDegrees(truncating: longs! as! NSNumber), timestamp: timeOf as! Int)
print("Latitude: (otherUsersLocations.coordinate.latitude), Longitude: (otherUsersLocations.coordinate.longitude), Time: (timeOf), Sender: (sender)")
I later call the function in the same func that I use to update the current users location. The code below is used to update the current users location to firebase.Firebase Database screenshot
let locationDictionary = [
"Sender": Auth.auth().currentUser?.email,
"Lat": latitude,
"Lon": longitude,
"Speed": speed,
"Time": ServerValue.timestamp()
] as [String : Any]
locationDB.childByAutoId().setValue(locationDictionary)
(error, reference) in
if error != nil
print(error!)
else
retrieveLocations()
I have tried creating a new "LocationModel" object but had troubles feeding the dictionary of LocationModel type with the retrieved data from firebase.
So my objective is to retrieve an array/dictionary of the location data for the paired user from "now" and subsequently measure the distance to the currentUser.
I've been sitting at this for a while so I really appreciate any suggestions.
Thanks a lot!
Pat
swift firebase firebase-realtime-database location data-retrieval
So I'm trying to retrieve the location of a "paired user" (i.e. not the current user but a contact of the current user) and monitor the distance travelled and distance relative to the currentUser. When using the code below I am only able to retrieve a list with multiple entries of the same location rather than consistently getting only the new location update of the 'paired user'.
func retrieveLocations() {
let locationDB = Database.database().reference().child("Locations").child("nameOfUser")
locationDB.queryLimited(toLast: 1).observe(.childAdded)
(snapshot) in
let snapshotValue = snapshot.value as! Dictionary<String,Any>
let lats = snapshotValue["Lat"]
let longs = snapshotValue["Lon"]
let timeOf = snapshotValue["Time"]
let sender = snapshotValue["Sender"]
let otherUsersLocations = CLLocation(latitude: CLLocationDegrees(truncating: lats! as! NSNumber), longitude: CLLocationDegrees(truncating: longs! as! NSNumber), timestamp: timeOf as! Int)
print("Latitude: (otherUsersLocations.coordinate.latitude), Longitude: (otherUsersLocations.coordinate.longitude), Time: (timeOf), Sender: (sender)")
I later call the function in the same func that I use to update the current users location. The code below is used to update the current users location to firebase.Firebase Database screenshot
let locationDictionary = [
"Sender": Auth.auth().currentUser?.email,
"Lat": latitude,
"Lon": longitude,
"Speed": speed,
"Time": ServerValue.timestamp()
] as [String : Any]
locationDB.childByAutoId().setValue(locationDictionary)
(error, reference) in
if error != nil
print(error!)
else
retrieveLocations()
I have tried creating a new "LocationModel" object but had troubles feeding the dictionary of LocationModel type with the retrieved data from firebase.
So my objective is to retrieve an array/dictionary of the location data for the paired user from "now" and subsequently measure the distance to the currentUser.
I've been sitting at this for a while so I really appreciate any suggestions.
Thanks a lot!
Pat
swift firebase firebase-realtime-database location data-retrieval
swift firebase firebase-realtime-database location data-retrieval
asked Nov 10 at 15:42
Spectrism12
61
61
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240547%2fstructuring-real-time-location-data-request-from-firebase-using-swift%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown