2016-12-05 49 views
1

我工作的一個應用程序,我希望存儲用戶的當前位置,當用戶註冊。如何等待didUpdateLocation方法調用其他方法之前叫什麼名字?

所以,一旦用戶點擊註冊按鈕locationManager.startUpdatingLocation()被調用,然後執行註冊操作。

然而,即使前didUpdateLocations委託方法返回一個座標的註冊方法被觸發,從而在用戶的位置被存儲在數據庫中的零。

試圖調用didUpdateLocations內的註冊的方法是,因爲亂七八糟,該委託被多次調用。

func signup() { 
     self.getLocation() 
     let point = PFGeoPoint(latitude:locationLat, longitude:locationLon) 
     ... 
     newUser[PF_USER_LOCATION] = point 
     newUser.signUpInBackground(block: { (succeed, error) -> Void in 

      if let errorInSignup = error { 
       ...  
      } else { 
       ... 
      } 
     }) 
    } 

    func getLocation() { 
     if CLLocationManager.locationServicesEnabled() {    
      locationManager.delegate = self 
      locationManager.desiredAccuracy = kCLLocationAccuracyBest 
      locationManager.startUpdatingLocation() 
     } 
    } 

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

     let locValue:CLLocationCoordinate2D = manager.location!.coordinate 

     locationLat = locValue.latitude 
     locationLon = locValue.latitude 
    } 

我只添加了部分代碼。

什麼是正確的程序要等到接收到的座標來調用註冊功能?

requestWhenInUseAuthorisation()是裏面的viewDidLoad()

+0

更改呼叫方法肯定的作品 –

+1

電話報名方式,只有當你收到當前位置,您可以使用關閉 – Diksha

+0

如果y你很高興只能定位ios9和更高版本,你可以使用'requestLocation' https://developer.apple.com/reference/corelocation/cllocationmanager/1620548-requestlocation否則你需要在'didUpdateLocations'中編寫代碼來檢查適當的水平精度,然後停止位置更新並完成註冊。您還需要爲用戶拒絕位置權限或在合理時間內無法確定位置提供適當的代碼 – Paulw11

回答

0

不喜歡在這裏

func signup() { 
     self.getLocation() 

    } 

呼叫

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
      manager.stoptUpdatingLocation() // initialy stop updation 
     let locValue:CLLocationCoordinate2D = manager.location!.coordinate 

     locationLat = locValue.latitude 
     locationLon = locValue.latitude 
     let point = PFGeoPoint(latitude:locationLat, longitude:locationLon) 
     ... 
     newUser[PF_USER_LOCATION] = point 
     newUser.signUpInBackground(block: { (succeed, error) -> Void in 

      if let errorInSignup = error { 
       ...  
      } else { 
       ... 
       // call your main view 
      } 
     }) 
    } 
+0

謝謝@ Anbu.karthik此外,我使用了locationmanager.startUpdatingLocations ()。這已經多次調用委託方法。在應該用locationManager.requestLocation()替換它以便只調用它一次。 – Sowndharya

+0

@Sowndharya - 按照保羅的意見也一次 –

相關問題