哪一个在iOS中的Swift中使用“locations array”或“manager.location.coordinate”来获取用户位置?

时间:2022-11-15 20:10:08

I want to get the user's location. It might be approximate position, that's fine. In didUpdateLocations method, i saw two ways to get the coordinates.

我想获得用户的位置。它可能是近似位置,没关系。在didUpdateLocations方法中,我看到了两种获取坐标的方法。

  1. Using manager.location.coordinate
  2. Using locations array
  3. 使用位置数组

Which one should I go for? I am thinking locations array will contain user's recent location as I am starting startUpdatingLocation() as soon as user opens the app.

我应该去哪一个?我认为位置数组将包含用户的最近位置,因为我在用户打开应用程序后立即启动startUpdatingLocation()。

So which one should i use to get the coordinates? And what does manager.location.coordinate do in this case?

那么我应该使用哪一个来获取坐标?在这种情况下,manager.location.coordinate做了什么?

Note: Accuracy of location need not be accurate to 10 meters. Rough estimation is enough.

注意:位置精度不必精确到10米。粗略估计就足够了。

Here is my code:

这是我的代码:

func initLocationUpdates() {
    if CLLocationManager.locationServicesEnabled() {
        print("going to get location")
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }
    else {
        print("location services disabled")
    }
}

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

    /*
    let userLocation:CLLocation = locations[0]  // or maybe even locations.last
    let long = userLocation.coordinate.longitude;
    let lat = userLocation.coordinate.latitude;
    print("lat: ", lat, " --- long: ", long)
    */

    locationManager.stopUpdatingLocation()
}

1 个解决方案

#1


1  

I'd prefer the array over the instance variable, because of the way desiredAccuracy works - it doesn't guarantee that the location updates you receive will have the accuracy you request - merely that the system will make its best effort to provide them. Depending on how the location manager behaves, you might get a number of updates in the locations array, with differing levels of accuracy. You can then filter the array to find the most accurate one.

我更喜欢数组而不是实例变量,因为desiredAccuracy的工作方式 - 它并不能保证你收到的位置更新会有你要求的准确性 - 只是系统会尽最大努力提供它们。根据位置管理器的行为方式,您可能会在位置数组中获得许多更新,但准确度不同。然后,您可以过滤数组以找到最准确的数组。

The key point is that if multiple updates arrive in the same didUpdateLocations: call, and you just use manager.location, you might be missing an update. The missing update would definitely not be the most recent one, but it might be the most accurate one received so far.

关键点在于,如果多个更新到达相同的didUpdateLocations:call,并且您只使用manager.location,则可能缺少更新。缺失的更新肯定不是最新的更新,但它可能是迄今为止收到的最准确的更新。

Since you have set desiredAccuracy to a very high level (ten meters), I'm guessing precision is more important to you than timing - it will take a while for the system to deliver an update of that level of accuracy (and it may never arrive if you are indoors or otherwise blocked from using GPS). So the likelihood of the scenario above occurring is reasonable.

由于你已经将期望的准确度设置到一个非常高的水平(十米),我猜测精度对你来说比时间更重要 - 系统需要一段时间才能提供这种精确度的更新(并且它可能永远不会如果您在室内或以其他方式阻止使用GPS,请到达。因此,上述情况发生的可能性是合理的。

As for the purpose of the instance variable location, the docs suggest that it's intended to be used in certain circumstances when the app restarts:

至于实例变量位置的目的,文档建议在应用程序重新启动时在某些情况下使用它:

In iOS 4.0 and later, this property may contain a more recent location object at launch time. Specifically, if significant location updates are running and your app is terminated, this property is updated with the most recent location data when your app is relaunched (and you create a new location manager object). This location data may be more recent than the last location event processed by your app.

在iOS 4.0及更高版本中,此属性可能在启动时包含更新的位置对象。具体来说,如果正在运行重要的位置更新并且您的应用程序已终止,则在重新启动应用程序(并创建新的位置管理器对象)时,将使用最新的位置数据更新此属性。此位置数据可能比您的应用处理的最后一个位置事件更新。

#1


1  

I'd prefer the array over the instance variable, because of the way desiredAccuracy works - it doesn't guarantee that the location updates you receive will have the accuracy you request - merely that the system will make its best effort to provide them. Depending on how the location manager behaves, you might get a number of updates in the locations array, with differing levels of accuracy. You can then filter the array to find the most accurate one.

我更喜欢数组而不是实例变量,因为desiredAccuracy的工作方式 - 它并不能保证你收到的位置更新会有你要求的准确性 - 只是系统会尽最大努力提供它们。根据位置管理器的行为方式,您可能会在位置数组中获得许多更新,但准确度不同。然后,您可以过滤数组以找到最准确的数组。

The key point is that if multiple updates arrive in the same didUpdateLocations: call, and you just use manager.location, you might be missing an update. The missing update would definitely not be the most recent one, but it might be the most accurate one received so far.

关键点在于,如果多个更新到达相同的didUpdateLocations:call,并且您只使用manager.location,则可能缺少更新。缺失的更新肯定不是最新的更新,但它可能是迄今为止收到的最准确的更新。

Since you have set desiredAccuracy to a very high level (ten meters), I'm guessing precision is more important to you than timing - it will take a while for the system to deliver an update of that level of accuracy (and it may never arrive if you are indoors or otherwise blocked from using GPS). So the likelihood of the scenario above occurring is reasonable.

由于你已经将期望的准确度设置到一个非常高的水平(十米),我猜测精度对你来说比时间更重要 - 系统需要一段时间才能提供这种精确度的更新(并且它可能永远不会如果您在室内或以其他方式阻止使用GPS,请到达。因此,上述情况发生的可能性是合理的。

As for the purpose of the instance variable location, the docs suggest that it's intended to be used in certain circumstances when the app restarts:

至于实例变量位置的目的,文档建议在应用程序重新启动时在某些情况下使用它:

In iOS 4.0 and later, this property may contain a more recent location object at launch time. Specifically, if significant location updates are running and your app is terminated, this property is updated with the most recent location data when your app is relaunched (and you create a new location manager object). This location data may be more recent than the last location event processed by your app.

在iOS 4.0及更高版本中,此属性可能在启动时包含更新的位置对象。具体来说,如果正在运行重要的位置更新并且您的应用程序已终止,则在重新启动应用程序(并创建新的位置管理器对象)时,将使用最新的位置数据更新此属性。此位置数据可能比您的应用处理的最后一个位置事件更新。