我正在尝试使用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方法的位置吗?
答案 0 :(得分:0)
您需要检测滚动地图的时间,可能是通过实施MKMapViewDelegate
中定义的userTrackingMode
方法。在此方法中,您需要将地图视图的.None
属性设置为theMap
。当用户平移或缩放func mapView(mapView: MKMapView!, regionWillChangeAnimated animated: Bool) {
if you want to stop tracking the user {
mapView.userTrackingMode = .None
}
}
变量时,将调用您的实现。因此,您应该尽可能地将实现保持为轻量级,因为对于单个平移或缩放手势可以多次调用它。
.Follow
如果您想再次开始关注用户的位置,请将该属性更改回.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
}
:
{{1}}