Swift 3 随用户方向旋转 map

标签 swift xcode rotation android-mapview

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

这是我当前的更新位置函数

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)
}

我应该在这里声明吗?

最佳答案

此示例会将 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)
    }
}

这就是你想要的吗?希望对您有所帮助:)

关于Swift 3 随用户方向旋转 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43672353/

相关文章:

objective-c - 你如何比较 Swift 中的 NSFontSymbolicTraits 和 NSFontBoldTrait?

ios - Xcode 6 iOS 7 View 大小

c - 物体通过世界轴而不是局部轴OpenGL旋转

swift - 代码 8 : Most Swift projects fails to build and compile

swift - 子类 View

Swift 静态属性初始化器是懒惰的,为什么我可以将它声明为常量

ios - AVFoundation 音频不会停止静音

c++ - xCode 8.1 GLFWWindow "first responder"问题

php - 使用 JS 和 PHP 裁剪和旋转图像

c# - 一旦物体撞到墙上,就从墙上反射一个物体