【问题标题】:How to show in which side user turn phone, if I show user location using MKMapView如果我使用 MKMapView 显示用户位置,如何显示用户在哪一侧打开电话
【发布时间】:2022-08-24 18:30:12
【问题描述】:

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

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

  • 这就是所谓的“标题”。

标签: ios swift mkmapview cllocationmanager userlocation


【解决方案1】:

更新这一行:

mapView.userTrackingMode = .followWithHeading

【讨论】:

  • 这行不通,我已经尝试过此信息stackoverflow.com/questions/39762732/… 但是,在我看来,这也行不通,我不明白为什么
  • 你是什​​么意思不起作用?您是否收到任何错误?或者它崩溃了?
  • 我从上面的链接尝试了所有方法,没有任何反应,我没有看到箭头或其他类型的东西,只是没有,我不明白哪里有问题,我尝试了几次,但它仍然不行,你觉得我哪里出错了?
  • 你看到用户位置圈了吗?
  • 不,不明白,为什么会这样,我也不明白,我检查了几次示例代码,并严格遵循指南
【解决方案2】:

此问题已通过answerAsteroid 的帮助解决,感谢您的支持。 我的代码:

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

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 2015-01-29
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多