2016-01-13 60 views
0

我正在嘗試製作一個應用程序,每個人都可以在同一個地圖上看到每個人的位置。我找不到關於如何在同一張地圖上檢索所有用戶位置的教程。Swift GeoPoint獲取所有用戶的位置ios

我必須設法使一個腳本,這個腳本上傳用戶的位置成解析:

PFGeoPoint.geoPointForCurrentLocationInBackground { 


      (geoPoint: PFGeoPoint?, Error : NSError?) -> Void in 
    if let geoPoint = geoPoint{ 

     PFUser.currentUser()? ["location"] = geoPoint 
    PFUser.currentUser()?.saveInBackground() 

我也設法獲取當前用戶的位置。

任何人都知道我如何顯示每個人的位置,不只是我的?

謝謝你的時間和幫助。我對Swift很陌生,所以讓我知道我是否需要提供更多信息。

這是我的顯示代碼。

PFGeoPoint.geoPointForCurrentLocationInBackground { 


      (geoPoint: PFGeoPoint?, Error : NSError?) -> Void in 
    if let geoPoint = geoPoint{ 

     PFUser.currentUser()? ["location"] = geoPoint 
    PFUser.currentUser()?.saveInBackground() 
self.MapView?.showsUserLocation = true 
     self.MapView?.delegate = self 
     MapViewLocationManager.delegate = self 
     MapViewLocationManager.startUpdatingLocation() 
    self.MapView?.setUserTrackingMode(MKUserTrackingMode.Follow, animated: false) 




    self.locationManager.requestAlwaysAuthorization() 

     self.locationManager.requestWhenInUseAuthorization() 

     if CLLocationManager.locationServicesEnabled(){ 

    self.locationManager.delegate = self 
      self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
      self.locationManager.startUpdatingLocation() 
     } 





     func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocationManager]){ 
     var locValue:CLLocationCoordinate2D = (manager.location?.coordinate)! 
     print("locations = \(locValue.latitude) \(locValue.longitude)") 

外ViewWDidLoad

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 


     let location = locations.last 
     let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 

     let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1)) 

     self.MapView?.setRegion(region, animated: true) 

     self.locationManager.stopUpdatingLocation() 



    } 
+0

誰是「大家」?你的應用的其他用戶? – Moritz

+0

是的。其他成員。 –

+0

那麼這不就是如何從Parse API獲取所有用戶的問題嗎?這不是在他們的文檔中的某處嗎?我試圖瞭解你的問題的實際性質。目前這個問題似乎對於SO非常廣泛和模糊。 :) – Moritz

回答

0

要得到所有用戶的位置並不像你想象的那麼簡單。最常見的應用程序正在後臺工作。在後臺應用程序可以在後臺或暫停或終止狀態(about states)。你必須從這些州的任何一個獲得位置。
這怎麼可能:您應該爲您的應用程序啓用背景模式以進行位置跟蹤。要做到這一點更簡單 - 爲所有用戶使用重要的位置跟蹤,但準確度將達到500米左右。
爲了得到更準確(但很難實現),您可以發送無聲推送通知用戶(about silent push),它終止或暫停狀態喚醒應用到後臺狀態,現在你可以發送用戶的當前位置到您的服務器。