Save to parse manually after data has been queried - ios

I am using Parse pinInBackground feature to pin data (image, text, date, coordinates) in the background and that data is queried every time the app is opened.The app is used to log a photo and the location and coordinates.So every entry you make is queried and displays in a tableview (only the count of entries yet).
I want to be able to let the user manually sink with Parse.com and not use the saveEventually feature.
Meaning I want a button and when pressed the queried data must sink with Parse and the be in pinned.
Here is how my data is pinned
#IBAction func submitButton(sender: AnyObject) {
locationLogs["title"] = log.title
locationLogs["description"] = log.descriptionOf
println("log = \(log.title)")
println("log = \(log.descriptionOf)")
locationLogs.pinInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if (success) {
}else{
println("error = \(error)")
}}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location:CLLocationCoordinate2D = manager.location.coordinate
let altitude: CLLocationDistance = manager.location.altitude
println("new long= \(location.longitude)")
println("new lat= \(location.latitude)")
println("new altitude= \(altitude)")
println("new timestamp = \(timestamp)")
locationLogs["longitude"] = location.longitude
locationLogs["latitude"] = location.latitude
locationLogs["altitude"] = altitude
locationLogs["timestamp"] = timestamp
}
And here I query it
var result : AnyObject?
override func viewDidLoad() {
super.viewDidLoad()
let query = PFQuery(className:"LocationLogs")
query.fromLocalDatastore()
query.findObjectsInBackgroundWithBlock( { (NSArray results, NSError error) in
if error == nil && results != nil {
self.result = results
println("count = \(self.result!.count)")
self.loggedItemsTableView.reloadData()
}else{
println("ERRRROOOORRRR HORROOORRR= \(error)")
}
})
}
I have tried to use this command:
result?.saveAllInBackground()
But this only gave back an error.
Can someone please give me the correct code on how to do this or give me a link showing me how.

Here is a full code explanation on how I solved it:
//create an Array
var tableData: NSArray = []
func queryAll() {
let query = PFQuery(className:"LocationLogs")
query.fromLocalDatastore()
query.findObjectsInBackgroundWithBlock( { (NSArray results, NSError error) in
if error == nil && results != nil {
println("array = \(results)" )
self.tableData = results!
self.loggedItemsTableView.reloadData()
}else{
println("ERRRROOOORRRR HORROOORRR= \(error)")
}
})
}
//Call save on the class and not the object
PFObject.saveAllInBackground(self.tableData as [AnyObject], block: { (success: Bool, error: NSError?) -> Void in
if (success) {
//Remember to unpin the data if it will no longer be needed
PFObject.unpinAllInBackground(self.tableData as [AnyObject])
println("Pinned Data has successfully been saved")
}else{
println("error= \(error?.localizedDescription)")
}
})

Use this to safely cast/checked your object
if let results = self.result { // this will verify if your self.result is a non-nil array of object
// if it has a value then it will be passed to results
// you can now safely proceed on saving your objects
PFObject.saveAllInBackground(results, block: { (succeeded, error) -> Void in
// additional code
if succeeded {
// alert, remove hud ......
}else{
if let reqError = error {
println(reqError.localizedDescription)
}
}
})
}

struct log {
// this is dummy datastructure that imitate some part of your code ... you dont need it
var title = ""
var descriptionOf = ""
}
struct LocationInfo{
var title:String!
var description:String!
var location:PFGeoPoint!
var timestamp:NSDate!
var username:String
}
var locationManager = CLLocationManager()
var arrayOfLocations = [LocationInfo]()
func submitButton(sender: AnyObject) {
var logData = log()
var point = OneLocation(locationManager)
let username = PFUser.currentUser()?.username
var locationLogs = PFObject(className: "")
locationLogs["title"] = logData.title
locationLogs["description"] = logData.descriptionOf
locationLogs["Location"] = point
locationLogs["username"] = username
locationLogs["TimeStamp"] = locationManager.location.timestamp
locationLogs.saveInBackgroundWithBlock { (success:Bool, error:NSError?) -> Void in
if error == nil
{
println("data was save")
}
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation (manager.location, completionHandler: {(placemarks, error)->Void in
var UserCurrentLocation:CLLocationCoordinate2D = manager.location.coordinate
// println("User's parking Location : \(UserCurrentLocation.latitude) \(UserCurrentLocation.longitude)")
if (error != nil) {
println("Error")
return
}
locationManager.stopUpdatingLocation()
})
}
func OneLocation(Manager:CLLocationManager)->PFGeoPoint{
var latitude = Manager.location.coordinate.latitude
var longitude = Manager.location.coordinate.longitude
var point = PFGeoPoint(latitude: latitude, longitude: longitude)
return point
}
func QueryFromParse(){
var query = PFQuery(className: "")
query.findObjectsInBackgroundWithBlock { (objects:[AnyObject]?, error:NSError?) -> Void in
if error == nil
{
if let new_objects = objects as? [PFObject]
{
for SingleObject in new_objects
{
// with this single object you could get the description, title , username,etc
var location = SingleObject["Location"] as! PFGeoPoint
var username = SingleObject["username"] as! String
var title = SingleObject["title"] as! String
var time = SingleObject["TimeStamp"] as! NSDate
var description = SingleObject["description"] as! String
var singleLocationInfo = LocationInfo(title: title, description: description, location: location, timestamp: time, username: username)
arrayOfLocations.append(singleLocationInfo)
// reload data for the tableview
}
}
}
else
{
println("error")
}
}
}
Therefore you have an arrayoflocations that can be used to populate data in your tableView
Struct log is a dummy datastructure
And also I don't know if that was what you wanted ... but you should get the idea..
Hope that helps..

Related

Swift & Firebase - Observer returning nil after changing data

A have a problem in my app. When i initialize the app on the first ViewController i get some data from my Firebase server using this code and a object called "By" and an array of objects called "byer":
func download() {
byer.removeAll()
self.Handle = self.ref?.child("Byer").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let by = By()
by.Latitude = dictionary["Latitude"]?.doubleValue
by.Longitude = dictionary["Longitude"]?.doubleValue
by.Name = snapshot.key
let coordinate = CLLocation(latitude: by.Latitude!, longitude: by.Longitude!)
let distanceInMeter = coordinate.distance(from: self.locationManager.location!)
by.Distance = Int(distanceInMeter)
byer.append(by)
byer = byer.sorted(by: {$0.Distance! < $1.Distance! })
DispatchQueue.main.async {
selectedCity = byer[0].Name!
self.performSegue(withIdentifier: "GoToMain", sender: nil)
}
}
})
}
This all works fine. But the problem comes when i later in the app chance the value in the database. I use a button with this code:
if byTextfield.text != "" && latitude != nil && longitude != nil {
ref?.child("Byer").child(byTextfield.text!).child("Latitude").setValue(latitude)
ref?.child("Byer").child(byTextfield.text!).child("Longitude").setValue(longitude)
}
But for some reason the app crashes and a red line comes over the line:
let coordinate = CLLocation(latitude: by.Latitude!, longitude: by.Longitude!)
From the download function in the top. And the text:
"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.".
I have tried to remove the observer using:
override func viewDidDisappear(_ animated: Bool) {
self.ref?.removeObserver(withHandle: self.Handle)
}
But this dosn't seems to help. Any suggestions?
using guard statement you can easily handle the nil value of the longitude and latitude. i.e
func download() {
byer.removeAll()
self.Handle = self.ref?.child("Byer").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let by = By()
guard let latitude = dictionary["Latitude"]?.doubleValue,let longitude =
dictionary["Longitude"]?.doubleValue else
{
return
}
by.Latitude = latitude
by.Longitude = longitude
by.Name = snapshot.key
let coordinate = CLLocation(latitude: by.Latitude!, longitude: by.Longitude!)
let distanceInMeter = coordinate.distance(from: self.locationManager.location!)
by.Distance = Int(distanceInMeter)
byer.append(by)
byer = byer.sorted(by: {$0.Distance! < $1.Distance! })
DispatchQueue.main.async {
selectedCity = byer[0].Name!
self.performSegue(withIdentifier: "GoToMain", sender: nil)
}
}
})
}
and if you want to unregister the observer from the firebase database reference then remove the database handler at the end of the childadded block.

iOS - How can I return latitude and longitude?

I can't return latitude and longitude. I always get a 0.0 and 0.0.
What can I do to get these values?
Code :
func forwardGeocoding (address: String) -> (Double, Double) {
let geoCoder = CLGeocoder()
var latitude: Double = 0.0
var longitude: Double = 0.0
geoCoder.geocodeAddressString(address) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in
if error != nil {
print(error?.localizedDescription)
} else {
if placemarks!.count > 0 {
let placemark = placemarks![0] as CLPlacemark
let location = placemark.location
latitude = Double((location?.coordinate.latitude)!)
longitude = Double((location?.coordinate.longitude)!)
print("before : \(latitude, longitude)")
}
}
}
print("after : \(latitude, longitude)")
return (latitude, longitude)
}
This my viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
forwardGeocoding("New York, NY, United States")
}
Result:
after : (0.0, 0.0)
before : (40.713054, -74.007228)
The problem with your code is that geocoding requests are asynchronous, so the return statement is executed before the geocoding results are actually retrieved.
I'd probably use one of two options to fix this. First, instead of returning a tuple, make your own completion handler, and call it after the placemark is found:
func forwardGeocoding (address: String, completion: (CLLocationCoordinate2D) -> Void) {
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(address) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in
if error != nil {
print(error?.localizedDescription)
} else {
if placemarks!.count > 0 {
let placemark = placemarks![0] as CLPlacemark
let location = placemark.location
completion(location.coordinate)
}
}
}
}
Now when you call this function you can provide the completion with the relevant values from wherever you're calling the function.
If the function is actually a method in a class and it never needs to be called from another class, you could have it set properties of the class, and those properties could have didSet blocks. For example:
class SomeClass {
var coordinates: CLLocationCoordinate2D {
didSet {
doSomethingWithCoordinates()
}
}
private func forwardGeocoding (address: String, completion: (CLLocationCoordinate2D) -> Void) {
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(address) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in
if error != nil {
print(error?.localizedDescription)
} else {
if placemarks!.count > 0 {
let placemark = placemarks![0] as CLPlacemark
let location = placemark.location
self.coordinates = location.coordinate
}
}
}
}
}
The first options is probably more versatile, but the second avoids having completion blocks withing completion blocks, which can sometimes become confusing to keep track of in your code.
It's quite obvious that your function returns always (0.0, 0.0). It's because geoCoder.geocodeAddressString() returns placemarks asynchronously.
It's time-consuming operation so you have to modify your code to handle that.
One of possible solutions is to modify your function:
func forwardGeocoding (address: String, completion: (Bool, CLLocationCoordinate2D!) -> () ) {
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(address) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in
if error != nil {
print(error?.localizedDescription)
completion(false,nil)
} else {
if placemarks!.count > 0 {
let placemark = placemarks![0] as CLPlacemark
let location = placemark.location
completion(true, location?.coordinate)
}
}
}
And call it:
self.forwardGeocoding(YourAdress, completion {
success, coordinate in
if success {
let lat = coordinate.lattitude
let long = coordinate.longitude
// Do sth with your coordinates
} else {
// error sth went wrong
}
}
Notice that your function returns nothing.
Hope that helps.

Healthkit HKAnchoredObjectQuery in iOS 9 not returning HKDeletedObject

I was excited to learn that Apple added tracking of deletes in HealthKit in iOS 9. So I set up a test project to try it out. Unfortunately, while I can get new data just fine, I am not getting any deleted objects in my callbacks.
I have a functioning HKAnchoredObjectQuery that tracks HKQueryAnchor and gives me new HKSamples whenever I add a BloodGlucose quantity into HealthKit via the Health app. However when I delete that same quantity and re-run the app the HKDeletedObject is always empty. Even I do an add and a delete at the same time. It seems no matter what I do, the HKDeletedObject array is always empty. But additions work fine (only getting the added samples since the last anchor).
Here is my code. It is just 2 files. To recreate the project just make a new swift project, give yourself the HealthKit Entitlement, and copy these in. (Note: When you run it, you only get one update for each run so if you make changes in HealthKit you have to stop and restart the app to test the callbacks.)
This is my HealthKit client:
//
// HKClient.swift
// HKTest
import UIKit
import HealthKit
class HKClient : NSObject {
var isSharingEnabled: Bool = false
let healthKitStore:HKHealthStore? = HKHealthStore()
let glucoseType : HKObjectType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodGlucose)!
override init(){
super.init()
}
func requestGlucosePermissions(authorizationCompleted: (success: Bool, error: NSError?)->Void) {
let dataTypesToRead : Set<HKObjectType> = [ glucoseType ]
if(!HKHealthStore.isHealthDataAvailable())
{
// let error = NSError(domain: "com.test.healthkit", code: 2, userInfo: [NSLocalizedDescriptionKey: "Healthkit is not available on this device"])
self.isSharingEnabled = false
return
}
self.healthKitStore?.requestAuthorizationToShareTypes(nil, readTypes: dataTypesToRead){(success, error) -> Void in
self.isSharingEnabled = true
authorizationCompleted(success: success, error: error)
}
}
func getGlucoseSinceAnchor(anchor:HKQueryAnchor?, maxResults:uint, callback: ((source: HKClient, added: [String]?, deleted: [String]?, newAnchor: HKQueryAnchor?, error: NSError?)->Void)!){
let queryEndDate = NSDate(timeIntervalSinceNow: NSTimeInterval(60.0 * 60.0 * 24))
let queryStartDate = NSDate.distantPast()
let sampleType: HKSampleType = glucoseType as! HKSampleType
let predicate: NSPredicate = HKAnchoredObjectQuery.predicateForSamplesWithStartDate(queryStartDate, endDate: queryEndDate, options: HKQueryOptions.None)
var hkAnchor: HKQueryAnchor;
if(anchor != nil){
hkAnchor = anchor!
} else {
hkAnchor = HKQueryAnchor(fromValue: Int(HKAnchoredObjectQueryNoAnchor))
}
let onAnchorQueryResults : ((HKAnchoredObjectQuery, [HKSample]?, [HKDeletedObject]?, HKQueryAnchor?, NSError?) -> Void)! = {
(query:HKAnchoredObjectQuery, addedObjects:[HKSample]?, deletedObjects:[HKDeletedObject]?, newAnchor:HKQueryAnchor?, nsError:NSError?) -> Void in
var added = [String]()
var deleted = [String]()
if (addedObjects?.count > 0){
for obj in addedObjects! {
let quant = obj as? HKQuantitySample
if(quant?.UUID.UUIDString != nil){
let val = Double( (quant?.quantity.doubleValueForUnit(HKUnit(fromString: "mg/dL")))! )
let msg : String = (quant?.UUID.UUIDString)! + " " + String(val)
added.append(msg)
}
}
}
if (deletedObjects?.count > 0){
for del in deletedObjects! {
let value : String = del.UUID.UUIDString
deleted.append(value)
}
}
if(callback != nil){
callback(source:self, added: added, deleted: deleted, newAnchor: newAnchor, error: nsError)
}
}
let anchoredQuery = HKAnchoredObjectQuery(type: sampleType, predicate: predicate, anchor: hkAnchor, limit: Int(maxResults), resultsHandler: onAnchorQueryResults)
healthKitStore?.executeQuery(anchoredQuery)
}
let AnchorKey = "HKClientAnchorKey"
func getAnchor() -> HKQueryAnchor? {
let encoded = NSUserDefaults.standardUserDefaults().dataForKey(AnchorKey)
if(encoded == nil){
return nil
}
let anchor = NSKeyedUnarchiver.unarchiveObjectWithData(encoded!) as? HKQueryAnchor
return anchor
}
func saveAnchor(anchor : HKQueryAnchor) {
let encoded = NSKeyedArchiver.archivedDataWithRootObject(anchor)
NSUserDefaults.standardUserDefaults().setValue(encoded, forKey: AnchorKey)
NSUserDefaults.standardUserDefaults().synchronize()
}
}
This is my View:
//
// ViewController.swift
// HKTest
import UIKit
import HealthKit
class ViewController: UIViewController {
let debugLabel = UILabel(frame: CGRect(x: 10,y: 20,width: 350,height: 600))
override func viewDidLoad() {
super.viewDidLoad()
self.view = UIView();
self.view.backgroundColor = UIColor.whiteColor()
debugLabel.textAlignment = NSTextAlignment.Center
debugLabel.textColor = UIColor.blackColor()
debugLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
debugLabel.numberOfLines = 0
self.view.addSubview(debugLabel)
let hk = HKClient()
hk.requestGlucosePermissions(){
(success, error) -> Void in
if(success){
let anchor = hk.getAnchor()
hk.getGlucoseSinceAnchor(anchor, maxResults: 0)
{ (source, added, deleted, newAnchor, error) -> Void in
var msg : String = String()
if(deleted?.count > 0){
msg += "Deleted: \n" + (deleted?[0])!
for s in deleted!{
msg += s + "\n"
}
}
if (added?.count > 0) {
msg += "Added: "
for s in added!{
msg += s + "\n"
}
}
if(error != nil) {
msg = "Error = " + (error?.description)!
}
if(msg.isEmpty)
{
msg = "No changes"
}
debugPrint(msg)
if(newAnchor != nil && newAnchor != anchor){
hk.saveAnchor(newAnchor!)
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.debugLabel.text = msg
})
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Note: I know Apple recommends to set this up using an HKObserverQuery. I originally did it that way in a Xamarin project and the behavior was the same (no HKDeletedObjects were getting sent). So when trying it out with swift I left out the HKObserverQuery for simplicity.
Remove the predicate (predicate: nil) when you instantiate the query and you'll see more results, including deleted results. The first time you run this (before the HKQueryAnchor has been saved) you'll get all the results so you may want to do something to filter those; but subsequent execution of the query will use your saved anchor so you'll only see changes since that saved anchor.
You probably also want to set the updateHandler property on your query before executing. This will set the query up to run continuously in the background calling the update handler whenever there are changes (Adds or Deletes). The code near the end of getGlucoseSinceAnchor(...) looks like
...
let anchoredQuery = HKAnchoredObjectQuery(type: sampleType, predicate: nil, anchor: hkAnchor, limit: Int(maxResults), resultsHandler: onAnchorQueryResults)
anchoredQuery.updateHandler = onAnchorQueryResults
healthKitStore?.executeQuery(anchoredQuery)
...

How come my array of CLLocation instances is empty?

I'm currently trying to construct a way that calculates the the distance in feet between my current location and other hard coded locations. I will eventually display these distances as strings in a table view. These are the steps that I'm taking:
1) I have a hard coded library of dictionaries that each hold a "latitude" and "longitude" key-value pair.
2) to extract this information from a struct as follows:
struct BarDetials {
// other properties
....
var lat: CLLocationDegrees?
var long CLLocationDegrees?
init(index: Int) {
// initialize other properties
// ...
lat = specificBarDetail["latitude"] as! CLLocationDegrees!
long = specificBarDetail["longitude"] as! CLLocationDegrees!
}
}
3) I use another struct to create an array of CLLocation instances from these coordinates as follows:
struct ConstructLocationsToCompare {
var latitude : [CLLocationDegrees?] = []
var longitude : [CLLocationDegrees?] = []
var barLocations : [CLLocation?] = []
init(){
for index in 0...21 {
var data = BarDetails(index: index)
if data.lat != nil {
latitude.append(data.lat!)
}
if data.long != nil {
longitude.append(data.long!)
}
barLocations[index] = CLLocation(latitude: latitude[index]!, longitude: longitude[index]!)
}
}
}
4) I then calculate the distances in my MasterViewController().
var latestLocation : AnyObject? , var currentLocation : CLLocation!, and var distanceStringArray : [String?] = [] are all properties of my MasterViewController() class.
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
// Get latest location and store it as a property of MasterViewController
if self.latestLocation != nil {
self.latestLocation = locations[locations.count - 1]
// Print out the latest locaiton for debuging
println("Latest Location")
println("---------------")
println(self.latestLocation)
// Create an instance of ContructLocationsToCompare to get all locaiton data
var getBarLocations = ConstructLocationsToCompare()
// Get loop to calculate the distance you are away from each Bar
var distanceDoubleArray : [Double?] = []
for index in 0...21 {
var distanceBetween : CLLocationDistance = self.latestLocation!.distanceFromLocation(getBarLocations.barLocations[index])
var distanceInFeet = distanceBetween * 3.28084
distanceDoubleArray[index] = distanceInFeet
self.distancesStringArray.append( String(format: "%6.0 ft", distanceDoubleArray[index]!))
}
}
//println(distanceBetween) or get error if it exists
if error != nil {
println("Error: " + error.localizedDescription)
return
}
if placemarks.count > 0 {
let pm = placemarks[0] as! CLPlacemark
self.displayLocationInfo(pm)
}
})
}
5) Lastly, to display my distance from each location (in my -cellForRowAtIndexPath):
println("")
println("")
println("List of Locations")
println("=================")
println(self.distancesStringArray)
println("")
println("")
println("")
println("")
if self.distancesStringArray[indexPath.row]!.isEmpty == true{
futureCell.distanceAway?.text = "-" // I crash at this line ^^^^^^
} else {
futureCell.distanceAway?.text = self.distancesStringArray[indexPath.row]
}
*** My distanceStringArray is always empty therefore I get a fatal error and the app crashes. How do I fix this? Is the fact that I declare var latestLocation : AnyObject? , var currentLocation : CLLocation!, and var distanceStringArray : [String?] = [] properties of my MasterViewController() class bad practice? If not do these properties need to be casted as a different type/ declared in a different manner?

how to retrive location from a PFGeopoint - (Parse.com and Swift) and show it on the map with Xcode 6.2

There are some answer on this, but related to different problems and all in objective-c.
I save in a parse class "Position" positions of users with this:
var locationManager = CLLocationManager()
var lat = locationManager.location.coordinate.latitude
var lon = locationManager.location.coordinate.longitude
let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon)
let myParseId = PFUser.currentUser().objectId //PFUser.currentUser().objectId
println("****** this is my geoPoint: \(myGeoPoint)")
func sendPosition(userOfPosition: User) {
let takePosition = PFObject(className: "Position")
takePosition.setObject(myParseId, forKey: "who") //who
takePosition.setObject(myGeoPoint, forKey: "where")
takePosition.saveInBackgroundWithBlock(nil)
}
sendPosition(currentUser()!)
so this is my result:
then I want to show them on map, but how? don't understand how to retrive latitude and longitude from "where" column the code below doesn't work:
import UIKit
import MapKit
class MapViewController: UIViewController {
#IBOutlet weak var mapView: MKMapView!
var locationManager : CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var locationManager = CLLocationManager()
var lat = locationManager.location.coordinate.latitude
var lon = locationManager.location.coordinate.longitude
let location = CLLocationCoordinate2D(latitude: lat, longitude: lon)
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
let anotation = MKPointAnnotation()
anotation.setCoordinate(location)
anotation.title = "my title"
anotation.subtitle = " my subtitle"
mapView.addAnnotation(anotation)
println("****** Welcome in MapViewController")
//MARK: (471) Crossing Positions
//*******************************************************
let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon)
let myParseId = PFUser.currentUser().objectId //PFUser.currentUser().objectId
println("****** this is my geoPoint from map view controller: \(myGeoPoint)")
//
// var inspector = PFQuery(className:"GameScore")
// inspector.saveInBackgroundWithBlock {
// (success: Bool, error: NSError?) -> Void in
// if (success) {
// // The object has been saved.
// var the = inspector.objectId
// } else {
// // There was a problem, check error.description
// }
// }
//
//
//
func filterByProximity() {
PFQuery(className: "Position")
.whereKey("where", nearGeoPoint: myGeoPoint, withinKilometers: 500.0) //(474)
.findObjectsInBackgroundWithBlock ({
objects, error in
if let proximityArray = objects as? [PFObject] {
println("****** here the proximity matches: \(proximityArray)")
for near in proximityArray {
println("here they are \(near)")
if let position = near["where"] as! PFGeoPoint {
let theirLat = position.latituide
let theirLon = position.longitude
}
let theirLat = near["where"].latitude as Double
let theirlong = near["where"].longitude as Double
let location = CLLocationCoordinate2DMake(theirLat, theirlong)
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegionMake(location, span)
self.mapView.setRegion(region, animated: true)
let theirAnotation = MKPointAnnotation()
theirAnotation.setCoordinate(location)
self.mapView.addAnnotation(anotation)
}
}
})
}
filterByProximity()
// //update my position
//
// func exists() {
// PFQuery(className:"Position")
// .whereKey("who", containsString: myParseId)
// .findObjectsInBackgroundWithBlock({
// thisObject, error in
// if let result = thisObject as? [PFObject] {
// println("here the result: \(result)")
//
// let gotTheId = result[0].objectId
// println("ecco l'id singolo \(gotTheId)")
//
// //******** update function ********
// var query = PFQuery(className:"Position")
// query.getObjectInBackgroundWithId(gotTheId) {
// (usingObject: PFObject?, error: NSError?) -> Void in
// if error != nil {
// println(error)
// } else if let objectToupdate = usingObject {
// println("else occurred")
// objectToupdate["where"] = myGeoPoint
// println("position should be updated")
// objectToupdate.saveInBackgroundWithBlock(nil)
// println("position should be saved")
//
// }
// }
// //******** end update function ********
// }
// })
// }
//
// exists()
//*******************************************************
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Update after first answers
tried to check for optionals, but have this message:
after not down casting the double:
the 'where' is a PFGeoPoint, you can just call latitude and longitude on them
Try something like this - I'm not 100% sure in Swift syntax, yet
if(geoPoint)
{
if(geoPoint!.latitude)
{
let latitude = geoPoint!.latitude as double;
}
}
this worked. Was both a matter of optionals, and a matter of variables, I was using the wrong ones:
//MARK: (471) Crossing Positions
//*******************************************************
let myGeoPoint = PFGeoPoint(latitude: lat, longitude:lon)
let myParseId = PFUser.currentUser().objectId //PFUser.currentUser().objectId
var radius = 100.0
println("****** this is my geoPoint from map view controller: \(myGeoPoint)")
//MARK: *** let's look for other users ***
var nearArray : [CLLocationCoordinate2D] = []
func filterByProximity() {
PFQuery(className: "Position")
.whereKey("where", nearGeoPoint: myGeoPoint, withinKilometers: radius) //(474)
.findObjectsInBackgroundWithBlock ({
objects, error in
if let proximityArray = objects as? [PFObject] {
// println("****** here the proximity matches: \(proximityArray)")
for near in proximityArray {
// println("here they are \(near)")
let position = near["where"] as? PFGeoPoint
var theirLat = position?.latitude //this is an optional
var theirLong = position?.longitude //this is an optional
var theirLocation = CLLocationCoordinate2D(latitude: theirLat!, longitude: theirLong!)
nearArray.append(theirLocation)
if nearArray.isEmpty {
println("*** ERROR! anyone close by ***")
} else
{
for person in nearArray {
let span = MKCoordinateSpanMake(2.50, 2.50)
let region = MKCoordinateRegionMake(theirLocation, span)
self.mapView.setRegion(region, animated: true)
let theirAnotation = MKPointAnnotation()
theirAnotation.setCoordinate(theirLocation)
theirAnotation.title = near["who"] as String
self.mapView.addAnnotation(theirAnotation)
}
}
}
println("****** in a radius of \(radius) there are \(nearArray.count) bikers ******")
}
})
}
filterByProximity()

Resources