Swift Userlocation不断更新,不允许我在地图中自由移动
让我解释一下。我已经创建了一个地图,其中显示了一些位置、PIN和用户位置。当我运行应用程序时,它会打开并放大跨度,然后我想在地图中移动,但应用程序会将视图拖回用户位置,甚至会放大到正常状态。我的问题是,我如何才能阻止我的应用程序一次又一次地这样做?因为这有点烦人。提前谢谢 我在这个Span过程中使用的函数如下Swift Userlocation不断更新,不允许我在地图中自由移动,swift,geolocation,userlocation,Swift,Geolocation,Userlocation,让我解释一下。我已经创建了一个地图,其中显示了一些位置、PIN和用户位置。当我运行应用程序时,它会打开并放大跨度,然后我想在地图中移动,但应用程序会将视图拖回用户位置,甚至会放大到正常状态。我的问题是,我如何才能阻止我的应用程序一次又一次地这样做?因为这有点烦人。提前谢谢 我在这个Span过程中使用的函数如下 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocati
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函数中,可以创建计数器。本质上,计数器要做的是更新和刷新用户在地图上的位置,以达到特定的迭代次数。之后,可以使用CLLocationManager的.stopUpdatengLocation方法。因此,对于您的情况,您将有如下内容:
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()
}
}
}