2016-12-05 50 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 - 按照保罗的意见也一次 –

相关问题