setUserTrackingMode to MKUserTrackingModeFollow without changing zoom level

∥☆過路亽.° 提交于 2019-12-11 02:25:59

问题


At Apple Documentation of setUserTrackingMode:animated:

it is stated that:

Setting the tracking mode to MKUserTrackingModeFollow or MKUserTrackingModeFollowWithHeading causes the map view to center the map on that location and begin tracking the user’s location. If the map is zoomed out, the map view automatically zooms in on the user’s location, effectively changing the current visible region.

My question, is there a way we can retain current zoom level on map, while setting the user tracking mode?


回答1:


No. One alternative is you could listen to user location updates yourself (either through Core Location or the MKMapViewDelegate methods and update the map center, but tracking mode can not guarantee to not change the zoom.




回答2:


The way i was able to do this was to call a MKCoordinateRegionMakeWithDistance call that uses the users location and center. I used 5000 for my values. This is what my test code looks like. `import UIKit import CoreLocation import 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)

}

} ` dont read any of the commented out locationManager info. The important thing here is to remember that calling setUserTrackingMode does not affect the zoom level but just moves the center to the users location so if you set a zoom level using the region and distance method, and then call setUserTrackingMode it will assume that zoom. This allows me to always zoom out to a reasonable zoom level each time we recenter and follow the users current location.



来源:https://stackoverflow.com/questions/28169356/setusertrackingmode-to-mkusertrackingmodefollow-without-changing-zoom-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!