我想显示用户位置和周围区域,但我也想允许用户在该区域周围平移。现在,如果我尝试滚动 map 上的其他地方,它会自动将我带回以用户为中心的基本区域。我该如何阻止呢?我想以用户为中心显示初始 View ,但我也希望能够四处滚动。在此先感谢你们的帮助!
导入 UIKit 导入 MapKit 导入核心位置
ViewControllerMain 类:UIViewController、MKMapViewDelegate、CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.startUpdatingLocation()
mapView.showsUserLocation = true
mapView.delegate = self
let longPress = UILongPressGestureRecognizer(target: self, action: "action:")
longPress.minimumPressDuration = 1.0
mapView.addGestureRecognizer(longPress)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let regionToZoom = MKCoordinateRegionMake(manager.location.coordinate, MKCoordinateSpanMake(0.01, 0.01))
mapView.setRegion(regionToZoom, animated: true)
}
最佳答案
您在 didUpdateLocations 中的代码正在重置区域。您有两个选择。
无论您是否已经设置了第一个位置,都存储在 ivar 中。仅当您还没有设置区域时。
设置一个运行 15 秒的计时器。如果 map 被用户移动,您将重置计时器。当计时器到期时,您可以重新定位到用户位置。
这将使 map 保持以用户为中心,但将使他们能够稍微四处平移以获取一些上下文。
关于ios - Swift Mapkit - 远离用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440448/