我正在尝试使用Xcode 6.3和swift做一个iOS应用程序。 我使用MKMapView来跟踪用户位置。 问题是,如果我滚动地图,我会立即返回到用户位置。 这是我的代码:
override func viewDidLoad() { super.viewDidLoad() manager = CLLocationManager() manager.delegate = self manager.desiredAccuracy = kCLLocationAccuracyBest manager.requestAlwaysAuthorization() manager.startUpdatingLocation() theMap.delegate = self theMap.mapType = MKMapType.Standard theMap.zoomEnabled = true theMap.addGestureRecognizer(longPress) theMap.scrollEnabled = true }和
func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) { let spanX = 0.007 let spanY = 0.007 var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY)) theMap.setRegion(newRegion, animated: false) theMap.scrollEnabled = true }如果我滚动地图,1秒后我返回到用户位置。 我应该更改setRegion方法的位置吗?
I'm trying to do an iOS app using Xcode 6.3 and swift. I use MKMapView for tracking user position. The problem is that if I scroll the map I immediately return to the user position. This is my code:
override func viewDidLoad() { super.viewDidLoad() manager = CLLocationManager() manager.delegate = self manager.desiredAccuracy = kCLLocationAccuracyBest manager.requestAlwaysAuthorization() manager.startUpdatingLocation() theMap.delegate = self theMap.mapType = MKMapType.Standard theMap.zoomEnabled = true theMap.addGestureRecognizer(longPress) theMap.scrollEnabled = true }and
func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) { let spanX = 0.007 let spanY = 0.007 var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY)) theMap.setRegion(newRegion, animated: false) theMap.scrollEnabled = true }If I scroll the map, after 1 sec I return to the user position. Should I change setRegion method position?
最满意答案
您需要检测滚动地图的时间,可能是通过实现MKMapViewDelegate定义的mapView(_:regionWillChangeAnimated:)方法。 在此方法中,您需要将地图视图的userTrackingMode属性设置为.None 。 当用户平移或缩放您的theMap变量时,将调用您的实现。 因此,您应该努力使实现尽可能轻量级,因为可以多次调用单个平移或缩放手势。
func mapView(mapView: MKMapView!, regionWillChangeAnimated animated: Bool) { if you want to stop tracking the user { mapView.userTrackingMode = .None } }如果要再次开始关注用户的位置,请将该属性更改回.FollowWithHeading或.FollowWithHeading :
enum MKUserTrackingMode : Int { case None // the user's location is not followed case Follow // the map follows the user's location case FollowWithHeading // the map follows the user's location and heading }You need to detect when you scroll the map, possibly by implementing the mapView(_:regionWillChangeAnimated:) method defined in MKMapViewDelegate. In this method you need to set the userTrackingMode property of your map view to .None. Your implementation will be called when the user pans or zooms your theMap variable. So you should strive to keep the implementation as lightweight as possible since it could be called many times for a single pan or zoom gesture.
func mapView(mapView: MKMapView!, regionWillChangeAnimated animated: Bool) { if you want to stop tracking the user { mapView.userTrackingMode = .None } }When you want to start following the user's location again, change that property back to either .Follow or .FollowWithHeading:
enum MKUserTrackingMode : Int { case None // the user's location is not followed case Follow // the map follows the user's location case FollowWithHeading // the map follows the user's location and heading }更多推荐
发布评论