Userlocation 不断更新,不会让我在地图上自由移动
Userlocation keeps updating and won´t let me move freely through my map
让我解释一下。我创建了一个地图,其中显示了一些位置(图钉)和用户位置。当我 运行 应用程序打开并放大(跨度)时,我想在地图中移动,但是应用程序 "drags" 视图返回到用户位置(它甚至放大回正常状态)。我的问题是,我怎样才能阻止我的应用程序一遍又一遍地这样做?因为这有点烦人。提前致谢。
我在这个 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 函数中,您可以创建一个计数器。本质上,计数器会做的是更新和刷新用户在地图上的位置以进行特定次数的迭代。之后,就可以使用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()
}
}
}
让我解释一下。我创建了一个地图,其中显示了一些位置(图钉)和用户位置。当我 运行 应用程序打开并放大(跨度)时,我想在地图中移动,但是应用程序 "drags" 视图返回到用户位置(它甚至放大回正常状态)。我的问题是,我怎样才能阻止我的应用程序一遍又一遍地这样做?因为这有点烦人。提前致谢。
我在这个 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 函数中,您可以创建一个计数器。本质上,计数器会做的是更新和刷新用户在地图上的位置以进行特定次数的迭代。之后,就可以使用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()
}
}
}