2015-10-30 31 views
3

我試圖查詢存儲在Parse後端的PFGeoPoint數組。我有用戶表,分配給它的數據如「位置」,「名稱」。 一切從我的應用程序張貼後發送到解析,並正確存儲在後端。我有問題從Parse中檢索所有位置並將它們存儲到地圖上的MKAnnotation中。 找到我下面的代碼在地圖解析中顯示所有用戶的地理位置

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)") 
 
      } 
 
     } 
 
    }

+0

如果嘗試使用此功能查詢存儲在解析類中的位置,會發生什麼? PFGeoPoint * userLocation = [PFGeoPoint geoPointWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude]; [查詢whereKey:@「location」nearGeoPoint:userLocation withinKilometers:radius]; –

+1

我在swift中有更多的知識......請你用迅捷的語言更新你的答案! – Mams1101

+2

代碼看起來很合理,但是當你嘗試使用它時,你不知道'MapViewLocationManager.location'是好還是有效的 - 它是什麼? – Wain

回答

1

而不是把呼叫你viewDidAppear()方法 - 如以前評論者說,你的位置可能是零,沒有返回結果 - 我會用一些跟蹤器進行排序並將其放入您的MKMapView委託方法的didUpdateLocations()中。在這裏,我使用GCD的dispatch_once(),這樣當我的位置第一次以合理的準確度被找到時,我的代碼就會被執行(在這裏你將把你的調用給Parse)。

申報GCD的跟蹤變量

var foundLocation: dispatch_once_t = 0

現在使用這樣的事情在你的位置管理的委託方法didUpdateLocations()

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

PS。您還應該考慮在主線程上對UI進行任何更新。 Parse會在後臺線程上獲取這些數據,儘管您可能永遠不會看到問題,但在主線程上執行任何UI更改都是更安全和更好的習慣。您可以通過以下方式執行此操作:

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