让我解释一下。我创建了一个 map ,其中显示了一些位置(图钉)和用户位置。当我运行该应用程序时,它会打开并放大(跨度),然后我想在 map 中移动,但该应用程序会将 View “拖”回用户位置(它甚至会放大回到正常状态)。我的问题是,我怎样才能阻止我的应用程序一遍又一遍地这样做?因为这有点烦人。提前致谢。
我在这个 Span 过程中使用的函数如下...
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake(1.0, 1.0)
let ubicacionUsuario: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(ubicacionUsuario, span)
mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = true
}
最佳答案
在 didUpdateLocations 函数中,您可以创建一个计数器。本质上,计数器会做的是更新和刷新用户在 map 上的位置以进行特定次数的迭代。之后,您可以使用 CLLocationManager
的 .stopUpdatingLocation
方法。所以,对于你的情况,你会有类似下面的内容:
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
// Top level class stuff, create CLLocationManager Instance
var manager = CLLocationManager()
// set initial value of counter to 0
var updateCount = 0
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//If the location has updated less than 3 times, continue to update the location
if updateCount < 3 {
let region: MKCoordinateRegion = MKCoordinateRegionMake(ubicacionUsuario, span)
mapView.setRegion(region, animated: true)
updateCount += 1
} else {
// Once the update has occurred a satisfactory number of times, prevent the update from automaitcally occuring again
manager.stopUpdatingLocation()
}
}
}
关于swift - Userlocation 不断更新,不会让我在 map 上自由移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44251506/