自动调用用户位置

User Location is automatically called

在我的代码中,我需要初始化我的用户位置:它工作正常。

但每当我想更改我的位置(通过选择地址或在屏幕上滑动)时,我的应用程序都会将我带回我的用户位置。

我在 that post 上找到了一些答案。 但是,我不打电话给 didUpdateUserLocation() 。所以我不知道该怎么办。

我的应用调用频繁吗ViewDidLoad()

这是我的代码,我在其中初始化我的位置。

var usrLocation: CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()
    if (CLLocationManager.locationServicesEnabled()) {
        usrLocation = CLLocationManager()
        usrLocation.delegate = self
        usrLocation.requestAlwaysAuthorization()
        usrLocation.requestLocation()
        usrLocation.startUpdatingLocation()
    }
    mapView.delegate = self
}

此外,我不能只删除我的 usrLocation.requestLocation() 功能,我需要在应用程序启动时以我的用户为中心。我想在其他地方调用它,但我不知道在哪里?

所以,正如 dan 所说,问题是我不应该调用 requestLocation()startUpdatingLocation().

所以现在我的代码就是这样,并且运行完美:

var usrLocation: CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()
    if (CLLocationManager.locationServicesEnabled()) {
        usrLocation = CLLocationManager()
        usrLocation.delegate = self
        usrLocation.requestAlwaysAuthorization()
        usrLocation.requestLocation()
    }
    mapView.delegate = self
}

你已经实现了
usrLocation.startUpdatingLocation()

这是在不断更新位置,所以只要您指向另一个位置,它就会更新用户位置。

删除此行代码将起作用。