繁体   English   中英

如果我使用 MKMapView 显示用户位置,如何显示用户在哪一侧转向

[英]How to show in which side user turn prone, if I show user location using MKMapView

我想显示用户将手机转到哪一侧,例如在 MapView 上,并且无法理解如何做到这一点,我尝试使用这些选项,但它们无法帮助我:

mapView.showsUserLocation = true
mapView.isRotateEnabled = true
mapView.isPitchEnabled = true
mapView.showsCompass = true
mapView.userTrackingMode = .follow

在此处输入图像描述

更新这一行:

mapView.userTrackingMode = .followWithHeading

这个答案Asteroid的帮助解决了这个问题,感谢您的支持。 我的代码:

import UIKit
import MapKit
import CoreLocation

class MapScreen: UIViewController, MKMapViewDelegate {
    
    
    let locationManager = CLLocationManager()
    var location: CLLocation!
    
    lazy var mapView: MKMapView = {
        let map = MKMapView()
        map.delegate = self
        
        return map
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(mapView)
        mapView.frame = view.frame
        
        initUserLocation()
    }
    
    var headingImageView: UIImageView?
    var userHeading: CLLocationDirection?
    
    func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
        if views.last?.annotation is MKUserLocation {
            addHeadingView(toAnnotationView: views.last!)
        }
    }
    
    func addHeadingView(toAnnotationView annotationView: MKAnnotationView) {
        if headingImageView == nil {
            let image = UIImage(named: "iconU")
            headingImageView = UIImageView(image: image)
            headingImageView!.frame = CGRect(x: (annotationView.frame.size.width - image!.size.width)/2, y: (annotationView.frame.size.height - image!.size.height)/2, width: image!.size.width, height: image!.size.height)
            annotationView.insertSubview(headingImageView!, at: 0)
            headingImageView!.isHidden = true
        }
    }
}

extension MapScreen: CLLocationManagerDelegate {
    
    
    func initUserLocation() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        locationManager.startUpdatingHeading()
        mapView.showsUserLocation = true
        //        mapView.userTrackingMode = .followWithHeading
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        self.location = locations.last as CLLocation?
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        if newHeading.headingAccuracy < 0 { return }
        
        let heading = newHeading.trueHeading > 0 ? newHeading.trueHeading : newHeading.magneticHeading
        userHeading = heading
        updateHeadingRotation()
    }
    
    
    func updateHeadingRotation() {
        if let heading = userHeading,
           let headingImageView = headingImageView {
            
            headingImageView.isHidden = false
            let rotation = CGFloat(heading/180 * Double.pi)
            headingImageView.transform = CGAffineTransform(rotationAngle: rotation)
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM