如果我使用MKMapCamera,指南针不会在MKMapView上工作

时间:2016-04-13 06:01:56

标签: ios xcode swift mkmapview mkmapcamera

所以我想做三件事。

  1. 有一张显示我所在位置的地图。
  2. 地图应略微倾斜以给出3D感觉;就像我们在苹果地图上用两根手指向上和向下滚动一样。
  3. 使用指南针旋转地图。
  4. 我已在地图上启用了userTrackingMode,地图会随着指南针一起旋转,但如果我在地图上设置了MKMapCamera,罗盘就无法工作。

    这是我的代码。

        override func viewDidLoad() {
                super.viewDidLoad()
                // Do any additional setup after loading the view, typically from a nib.
                locationManager.desiredAccuracy = kCLLocationAccuracyBest
                locationManager.delegate = self
                locationManager.requestWhenInUseAuthorization()
                mapView.setUserTrackingMode(.FollowWithHeading, animated: true)
                locationManager.startUpdatingLocation()
    
            }
    
    
    func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
    
    
                let userCordinate = CLLocationCoordinate2D(latitude: newLocation.coordinate.latitude, longitude: newLocation.coordinate.longitude)
    
                let eyeCordinate = CLLocationCoordinate2D(latitude: newLocation.coordinate.latitude - 0.021078, longitude: newLocation.coordinate.longitude - 0.04078 )
    
    
    
                let mapCamera = MKMapCamera(lookingAtCenterCoordinate: userCordinate, fromEyeCoordinate: eyeCordinate, eyeAltitude: 1400)
    
                mapView.setCamera(mapCamera, animated: true)
                print("Camera set")
    
            }
    

    我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

您必须使用指南针更新摄像机的标题。这是我目前正在使用的东西:

func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        self.mapView.camera.heading = newHeading.trueHeading
}