ios - MapKit View 未放大到用户位置

标签 ios swift

在我的应用程序中,我有一个 MKMapKit View ,当我的应用程序启动时,如果用户允许位置服务,我希望 map 放大到用户的位置。我写的代码是:

override func viewDidLoad() {
    super.viewDidLoad()

    mapKitView.delegate = self
    mapKitView.showsUserLocation = true
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.delegate = self

    if (CLLocationManager.locationServicesEnabled()) {

        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
    }

    locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.startUpdatingLocation()
    }

    DispatchQueue.main.async {
        self.locationManager.startUpdatingLocation()
    }
}


func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    let noLocation = CLLocationCoordinate2D()
    let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200)
    mapKitView.setRegion(viewRegion, animated: false)
}

在我的应用中,它显示了用户的位置,但没有动画和放大。

最佳答案

使用用户的位置来计算 View 区域并将animated参数更改为true:

if let location = mapKitView.userLocation.location {
    let viewRegion = MKCoordinateRegionMakeWithDistance(location, 200, 200)
    mapKitView.setRegion(viewRegion, animated: true)
}

关于ios - MapKit View 未放大到用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384410/

相关文章:

ios - Xcode 11.2 - 在钥匙串(keychain)中找不到指定的项目

ios - 使用自定义单元格反转 UITableView

iphone - TabBarController + NavigationController :Push and Pop issue

objective-c - Xcode,为什么会自动添加一些约束?

swift - 基于非等同数据结构比较包含实际等同类型的字典

ios - ABBYY Cloud OCR SDK 返回空的 authToken 作为响应?

ios - 当#define一个变量时,@interface @实现和.h有什么不同

ios - 监听队列中请求的最佳服务架构(基于队列的负载均衡)?

ios - 如何在屏幕上交换文本字段的位置 - Swift

string - '[字符串 ]' does not have a member named ' 文本' Swift