setUserTrackingMode to MKUserTrackingModeFollow而不更改缩放级别

问题描述 投票:2回答:2

setUserTrackingMode:animated:的Apple文档中

据说:

将跟踪模式设置为MKUserTrackingModeFollow或MKUserTrackingModeFollowWithHeading会使地图视图将地图置于该位置的中心,并开始跟踪用户的位置。如果地图缩小,地图视图会自动放大用户的位置,从而有效地更改当前可见区域。

我的问题是,在设置用户跟踪模式的同时,我们是否可以在地图上保留当前缩放级别?

ios objective-c mkmapview mapkit
2个回答
0
投票

没有。另一种选择是您可以自己收听用户位置更新(通过核心位置或MKMapViewDelegate方法并更新地图中心,但跟踪模式无法保证不会更改缩放。


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,它将假定缩放。这使我每次重新定位并跟随用户当前位置时总是缩小到合理的缩放级别。

© www.soinside.com 2019 - 2024. All rights reserved.