在setUserTrackingMode:animated:
的Apple文档中声明:
将跟踪模式设置为MKUserTrackingModeFollow或MKUserTrackingModeFollowWithHeading会使地图视图将地图置于该位置的中心,并开始跟踪用户的位置。如果地图缩小,地图视图会自动放大在用户的位置,从而有效地更改当前可见区域。
我的问题是,我们是否可以在设置用户跟踪模式的同时保留地图上的当前缩放级别?
答案 0 :(得分:0)
没有。另一种方法是您可以自己收听用户位置更新(通过核心位置或MKMapViewDelegate
方法并更新地图中心,但跟踪模式无法保证不会更改缩放。
答案 1 :(得分:0)
我能够做到这一点的方法是调用使用用户位置和中心的MKCoordinateRegionMakeWithDistance调用。我使用5000作为我的价值观。这就是我的测试代码。 `import UIKit 导入CoreLocation 导入MapKit
class ViewController: UIViewController, CLLocationManagerDelegate{
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
if (CLLocationManager.locationServicesEnabled()){
mapView.showsUserLocation = true
mapView.mapType = MKMapType.satellite
mapView.setUserTrackingMode(MKUserTrackingMode.followWithHeading, animated: true)
//locationManager = CLLocationManager()
//locationManager.delegate = self
//locationManager.desiredAccuracy = kCLLocationAccuracyBest
//locationManager.requestAlwaysAuthorization()
//locationManager.startUpdatingLocation()
}
}
/*func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as! CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
//let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
let region = MKCoordinateRegionMakeWithDistance(center, 500, 500)
self.mapView.setRegion(region, animated: true)
}*/
@IBAction func centerButton(_ sender: UIButton, forEvent event: UIEvent) {
let location = MKUserLocation()
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegionMakeWithDistance(center, 5000, 5000)
self.mapView.setRegion(region, animated: true)
mapView.setUserTrackingMode(MKUserTrackingMode.followWithHeading, animated: true)
}
} ` 不读任何注释掉的locationManager信息。这里重要的是要记住调用setUserTrackingMode不会影响缩放级别,只是将中心移动到用户位置,所以如果使用region和distance方法设置缩放级别,然后调用setUserTrackingMode,它将假定缩放。这使我每次重新定位并跟随用户当前位置时总是缩小到合理的缩放级别。
答案 2 :(得分:0)
从iOS 13开始,您可以设置最低地图缩放级别,以防止MKUserTrackingModeFollow在设置后比指定距离更近:
let minZoom: CLLocationDistance = 10000 // desired visible radius from user in metres
let zoomRange = MKMapView.CameraZoomRange(minCenterCoordinateDistance: minZoom)
mapView.setCameraZoomRange(zoomRange, animated: true)
https://developer.apple.com/documentation/mapkit/mkmapview/camerazoomrange