地图视图相机航向问题

Issues with map view camera heading

我在设置 UIMapView 时遇到问题 order/way。这就是我希望发生的事情:

显示视图 - 地图旋转到指定航向

点击重置按钮 - 如果用户移动了地图,它将重置为默认航向和缩放

目前地图出现时旋转到航向,但重置按钮没有任何作用。我怀疑这取决于我做事的顺序,因为如果我翻转两行代码,它就可以工作,但是当地图出现时它不会旋转到正确的航向。

这是我的代码:

@IBAction func rotateToDefault(sender: AnyObject) {
    mapView.setRegion(zoomRegion, animated: true)
    mapView.camera.heading = parkPassed.orientation!
}

override func viewWillAppear(animated: Bool) {
    setUpMapView()
}

override func viewDidAppear(animated: Bool) {
    mapView.setRegion(zoomRegion, animated: true)
    mapView.camera.heading = parkPassed.orientation!
}

func setUpMapView() {
    rideArray = ((DataManager.sharedInstance.rideArray) as NSArray) as! [Ride]

    zoomRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2D(latitude: parkPassed.latitude!, longitude: parkPassed.longitude!), 1000, 1000)
    mapView.setRegion(zoomRegion, animated: true)
    mapView.delegate = self

    for ride in rideArray {
        var subtitle = ""
        if locationManager.location == nil {
            subtitle = "Distance unavailable"
        } else {
            let userLocation = CLLocation(latitude: locationManager.location.coordinate.latitude, longitude: locationManager.location.coordinate.longitude)
            let annotationLocation = CLLocation(latitude: ride.latitude!, longitude: ride.longitude!)

            var distance = Int(CLLocationDistance(annotationLocation.distanceFromLocation(userLocation)))

            if distance > 1000 {
                distance = distance / 1000
                subtitle = "\(distance) kilometers"
            } else {
                subtitle = "\(distance) meters"
            }
        }

        let annotation = RideAnnotation(coordinate: CLLocationCoordinate2DMake(ride.latitude!, ride.longitude!), title: ride.name!, subtitle: subtitle)
        self.qTree.insertObject(annotation)
        annotationsAdded.insertObject(annotation, atIndex: 0)

        println(qTree.count)
    }
}

有人有什么建议吗?

我遇到了类似的问题。

看来区域和相机是两个互斥的概念,它们决定了您如何查看地图的哪一部分。

如果你使用区域,你有坐标和跨度来确定你看到的东西(你已经在你的代码中这样做了)

如果您使用相机,您可以使用坐标、距离、间距和航向来确定您如何查看地图。

使用 mapView.setCamera(...) 平滑地更改您看到的内容,包括标题。

要定义您的相机视图,您可以执行以下操作

let camera = MKMapCamera(lookingAtCenterCoordinate: userLocation, fromDistance: 1000, pitch: 0, heading: heading)
self.mapView.setCamera(camera, animated: false)

来自苹果文档:

Assigning a new camera to this property updates the map immediately and without animating the change. If you want to animate changes in camera position, use the setCamera:animated: method instead.