Swift 3使用用户方向旋转地图

时间:2017-04-28 05:08:23

标签: swift xcode rotation android-mapview

目前我的swift应用程序有一个带有各种注释的mapview,当用户从地图上的各种注释走到不总是锁定在北方时,有没有办法?我希望地图转向他们走的方向而不是总是指向北方。

这是我当前的更新位置功能

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{

    let location = locations[0]
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, noteTime.span)
    map.setRegion(region, animated: true)
    self.map.showsUserLocation = true
    map.tintColor = UIColor(red:0.19, green:0.69, blue:0.73, alpha:1.0)
}

我应该在这里申报吗?

1 个答案:

答案 0 :(得分:5)

此示例将更新mapView的旋转,使其指向与用户相同的方向。

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.startUpdatingHeading()
    }

    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        mapView.camera.heading = newHeading.magneticHeading
        mapView.setCamera(mapView.camera, animated: true)
    }
}

那是你的追求吗?我希望有帮助:)