【问题标题】:How can I use setregion() to set region when user location is moving only?当用户位置仅移动时,如何使用 setregion() 设置区域?
【发布时间】:2020-09-26 20:50:15
【问题描述】:

我已尝试删除我的 setRegion(),但它仍然不允许地图平移。该构建覆盖用于探索地图视图的手指手势,并在恢复到原始位置之前非常简短地做出响应。我不确定这个问题。我已经将我的代码重写为一个新项目并且没有问题,谁能向我解释我哪里出错了?

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController {

    @IBOutlet weak var mapView: MKMapView!
    
    let locationManager = CLLocationManager()
    let regionInMeters: Double = 15000
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        checkLocationServices()
    }

    
    func setupLocationManager(){
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }
    
    func centerUserLocation(){
        if let location = locationManager.location?.coordinate{
            let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
            mapView.setRegion(region, animated: true)
        }
    }

    func checkLocationServices(){
        if CLLocationManager.locationServicesEnabled(){
            setupLocationManager()
            checkLocationAuthorization()
        } else {
            // alert user must turn on
        }
    }
    
    func checkLocationAuthorization(){
        switch CLLocationManager.authorizationStatus(){
        case .authorizedWhenInUse:
            mapView.showsUserLocation = true
            centerUserLocation()
            locationManager.startUpdatingLocation()
            break
        case .denied:
            mapView.showsUserLocation = false
            break
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
            break
        case .restricted:
            //supervisory controls enabled
            mapView.showsUserLocation = false
            break
        case .authorizedAlways:
            mapView.showsUserLocation = true
            break
        }
    }
    
}

extension ViewController: CLLocationManagerDelegate{
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last else {return}
        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        let region = MKCoordinateRegion.init(center: center, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
        mapView.setRegion(region, animated: true)
    }
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        checkLocationAuthorization()
    }
}

【问题讨论】:

  • 分享一段你的代码,让你的问题更清楚
  • 你在地图视图上设置了什么userTrackingMode
  • @sekoya 添加代码
  • @Paulw11 我不记得明确设置了跟踪模式

标签: ios swift iphone xcode


【解决方案1】:

您在didUpdateLocations 中调用setRegion - 这将在您每次获得位置更新时更改地图区域;大约每秒一次,精度为best;即使用户没有移动

如果您希望在您的地图视图上跟踪用户,您应该使用内置的userTrackingMode 功能。

【讨论】:

  • 我这样做是因为我预计会添加导航/方向。但就目前而言,如果我在地图视图上滑动(在构建时),它会在设定的区域高度自动返回用户位置。你是说我应该在其他地方调用 set region 吗?
  • 只有在要设置地图区域时才调用setRegion。目前,每次收到位置更新委托呼叫时,您都会继续设置它。您需要决定在什么条件下使地图居中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-01
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多