ios - Swift Mapkit - 远离用户位置

标签 ios xcode swift mapkit

我想显示用户位置和周围区域,但我也想允许用户在该区域周围平移。现在,如果我尝试滚动 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 中的代码正在重置区域。您有两个选择。

  1. 无论您是否已经设置了第一个位置,都存储在 ivar 中。仅当您还没有设置区域时。

  2. 设置一个运行 15 秒的计时器。如果 map 被用户移动,您将重置计时器。当计时器到期时,您可以重新定位到用户位置。

这将使 map 保持以用户为中心,但将使他们能够稍微四处平移以获取一些上下文。

This answer shows how to do it in Objective-C

关于ios - Swift Mapkit - 远离用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440448/

相关文章:

ios - 具有自定义布局的 UICollectionView 界面旋转

ios - 从服务器缓存 mp4 视频以与 avplayer 一起使用

ios - 如何在用户更改 UITableViewCell 中的值时更新 UITableView 数据源

ios - 路由应用程序覆盖文件可选且不包含在内,但现在我无法保存

ios - 如何在 Swift 中将字典数组存储到 Realm 数据库

ios - 在 Swift 中重写 layout.subviews 的删除控制

objective-c - 在幕后发送电子邮件

ios - 存储大字符串数组时编译器错误的解决方法

ios - 如何通过点击 tableview 单元格内的标签来呈现另一个 View Controller

ios - Xcode 9 - 修复范围内的所有内容变灰