swift - Userlocation 不断更新,不会让我在 map 上自由移动

标签 swift geolocation userlocation

让我解释一下。我创建了一个 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/

相关文章:

swift - 在 userLocation 上创建新的位置指示器

ios - UIButton 不响应嵌套在另一个 UIScrollView 中的 UIScrollView 中的触摸

java - 将 IP 地址范围转换为两个 BIGINT 以加快查找速度

iphone - MKUserLocation 蓝色 userLocation MKAnnotation 如果不经意触摸会导致应用程序崩溃

ios - Mapbox iOS : User location not showing after turning device location service on

javascript - 如果被拒绝,再次请求地理定位许可

ios - 如何使用 AVPlayer 获取 HLS 流的当前时间?

swift - 获取当前时间作为字符串 swift 3.0

swift - 在 Swift 中按住按钮时移动对象

python - 在用户的经度/纬度 20 英里范围内查找经度/纬度