我对使用CoreLocation和Parse的理解相当新,所以请耐心等待!我用swift和很多SO帖子进行编码,我一直在搜索Obc-C,我对此非常不熟悉。
............................................ ..................... UPDATE ............................ ........................................... 尝试将当前用户位置存储到Parse数据库中但不起作用。我收到错误:
[错误]:会话令牌无效(代码:209,版本:1.9.1) 可选(错误域= kCLErrorDomain代码= 0"(null)")"
有任何建议/帮助指出我正确的方向吗?
import UIKit
import Parse
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var manager: CLLocationManager!
var activityIndicator:UIActivityIndicatorView = UIActivityIndicatorView()
@IBOutlet weak var username: UITextField!
@IBOutlet weak var password: UITextField!
@IBAction func signUp(sender: AnyObject) {
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
let user = PFUser()
user.username = username.text
user.password = password.text
user.signUpInBackgroundWithBlock { (success, error) -> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if error == nil {
PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGeoPoint?, error: NSError?) -> Void in
if error == nil {
print("got location successfully")
PFUser.currentUser()!.setValue(geoPoint, forKey:"location")
PFUser.currentUser()!.saveInBackground()
} else {
print(error)
}
}
self.performSegueWithIdentifier("login", sender: self)
} else {
print(error)
}
}
}
@IBAction func login(sender: AnyObject) {
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
PFUser.logInWithUsernameInBackground(username.text!, password: password.text!) { (success, error) -> Void in
if error == nil {
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
self.performSegueWithIdentifier("login", sender: self)
} else {
print(error)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
答案 0 :(得分:2)
我建议将geopoint存储在Cobol+Hibernate
课程中。此外,您应该向创建它的User
添加 指针 ,而不是向每个Post
添加地理点。这消除了两次存储位置的冗余,并确保您不必混淆保持两个变量同步。此外,这也比直接存储objectId更好。
更新您的User
课程,以获得名为Post
的{{1}}指针列。然后而不是
User
使用
user
值得指出的是,Parse包括helper methods to get the current user's locations。它就像下面的
一样简单postEvent["userId"] = PFUser.currentUser()!.objectId!
如果您决定自己处理postEvent["user"] = PFUser.currentUser()!
,请确保在更新用户位置后致电PFGeoPoint.geoPointForCurrentLocationInBackground {
(geoPoint: PFGeoPoint?, error: NSError?) -> Void in
if error == nil {
// do something with the new geoPoint
}
}
以节省电池续航时间,假设您不需要连续的位置更新。< / p>
从那里,您可以使用query constraints和compound query来获取所有附近的CLLocationManager
个对象。特别是,您需要使用stopUpdatingLocation()
作为复合查询,该查询仅计为1个API请求。
Post