简体   繁体   English

在地图解析中显示所有用户的地理位置

[英]Displaying geopoint of all users in a map parse

I'am trying to query an array of PFGeoPoints stored on the Parse backend. 我试图查询存储在Parse后端的PFGeoPoints数组。 I have the User table, with data assigned to it such as "location", "name". 我有User表,并为其分配了数据,例如“location”,“name”。 everything is being sent to Parse upon posting from my app and is properly stored in the backend. 从我的应用程序发布后,所有内容都被发送到Parse并正确存储在后端。 I am having issues retrieving all location from Parse and storing them into an MKAnnotation on the map. 我在从Parse检索所有位置并将它们存储到地图上的MKAnnotation时遇到问题。 Find below my code 在下面找到我的代码

 import UIKit import Parse import CoreLocation import MapKit class mapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { @IBOutlet var mapUsers: MKMapView! var MapViewLocationManager:CLLocationManager! = CLLocationManager() var currentLoc: PFGeoPoint! = PFGeoPoint() override func viewDidLoad() { super.viewDidLoad() // ask user for their position in the map PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGeoPoint?, error: NSError?) -> Void in if let geoPoint = geoPoint { PFUser.currentUser()? ["location"] = geoPoint PFUser.currentUser()?.save() } } mapUsers.showsUserLocation = true mapUsers.delegate = self MapViewLocationManager.delegate = self MapViewLocationManager.startUpdatingLocation() mapUsers.setUserTrackingMode(MKUserTrackingMode.Follow, animated: false) } override func viewDidAppear(animated: Bool) { let annotationQuery = PFQuery(className: "User") currentLoc = PFGeoPoint(location: MapViewLocationManager.location) annotationQuery.whereKey("Location", nearGeoPoint: currentLoc, withinMiles: 10) annotationQuery.findObjectsInBackgroundWithBlock { (PFUser, error) -> Void in if error == nil { // The find succeeded. print("Successful query for annotations") let myUsers = PFUser as! [PFObject] for users in myUsers { let point = users["Location"] as! PFGeoPoint let annotation = MKPointAnnotation() annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude) self.mapUsers.addAnnotation(annotation) } } else { // Log details of the failure print("Error: \\(error)") } } } 

Instead of putting the call in your viewDidAppear() method - as previous commenters said, your location may be nil, returning no results - I would use a tracker of some sort and put it in your didUpdateLocations() MKMapView delegate method. 而不是将调用放在你的viewDidAppear()方法中 - 正如之前的评论者所说,你的位置可能是零,没有返回任何结果 - 我会使用某种跟踪器并将它放在你的didUpdateLocations() MKMapView委托方法中。 Here, I use GCD's dispatch_once() so that when my location is found for the first time with a reasonable accuracy, my code is executed (here you will put your call to Parse). 在这里,我使用GCD的dispatch_once()这样当我第一次以合理的准确度找到我的位置时,我的代码就被执行了(这里你将调用Parse)。

Declare GCD's tracker variable 声明GCD的跟踪变量

var foundLocation: dispatch_once_t = 0

Now use something like this in your location manager's delegate method didUpdateLocations() 现在在您的位置管理器的委托方法中使用类似的东西didUpdateLocations()

    if userLocation?.location?.horizontalAccuracy < 2001 {
        dispatch_once(&foundLocation, {
            // Put your call to Parse here
        })
    }

PS. PS。 You should also consider doing any updates to UI on the main thread. 您还应该考虑在主线程上对UI进行任何更新。 Parse fetches that data on a background thread and although you might never see a problem, it's both safer and good habit to do any UI changes on main thread. Parse在后台线程上获取数据,虽然您可能永远不会看到问题,但在主线程上进行任何UI更改都是更安全和更好的习惯。 You can do this with the following: 您可以使用以下内容执行此操作:

dispatch_async(dispatch_get_main_queue(), {
    // Put any UI update code here. For example your pin adding
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM