-1

I want to show into which side the user turns the phone, like on MapView, and can't understand how to do that, I have tried to use these options, but they can't help me:

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

enter image description here

Ice
  • 680
  • 1
  • 10
  • 23

2 Answers2

0

Update this line:

mapView.userTrackingMode = .followWithHeading
Asteroid
  • 1,049
  • 2
  • 8
  • 16
  • This not work, I have tried this info https://stackoverflow.com/questions/39762732/ios-10-heading-arrow-for-mkuserlocation-dot/40808645#40808645 but, also on my point this does not work, and I can't understand why – Ice Jun 03 '22 at 08:17
  • What do you mean doesn't work? Are you receiving any errors? Or it crashes? – Asteroid Jun 03 '22 at 15:45
  • I have tried all ways from the link above, and nothing happened, I haven't seen an arrow or another kind of something, just nothing, and I can't understand where was a problem, I tried a few times, but it still not work, how do you think where I make the mistakes? – Ice Jun 03 '22 at 16:00
  • Do you see user location circle? – Asteroid Jun 03 '22 at 17:19
  • No don't see, and why this happened, I also do not understand, I check examples code few times, and follow guideline strongly – Ice Jun 04 '22 at 18:51
  • Please share your code on how you are setting up the location manager (CLLocationManager). – Asteroid Jun 04 '22 at 19:56
  • This is my basic code without any help info from link, you can copy and run it https://drive.google.com/file/d/1TYV4ElMoIGlbYf9HRMtRhLOXiurz4jdV/view?usp=sharing – Ice Jun 04 '22 at 21:26
  • I ran your code and it's showing the location and heading correctly. Two things, first make sure you have added "Privacy - Location When In Use Usage Description" in your info.plist. Secondly, you should try it on real device, not simulator. – Asteroid Jun 04 '22 at 21:44
  • Which of these examples are you testing? https://stackoverflow.com/questions/39762732/ios-10-heading-arrow-for-mkuserlocation-dot/ Each time I run all examples on real device, and all access exist in info.plist, can you share your code? My code show only location ( – Ice Jun 05 '22 at 08:39
  • I did recreate the project again and add this implementation of the heading, https://stackoverflow.com/questions/39762732/ios-10-heading-arrow-for-mkuserlocation-dot/40808645#40808645, and can say that all work, if be honest don't know what was changed, but I guess the problem was inside basis installation of the CLLocationManager, I will bring the project to a good example, and post my answer here for a closing question, will be appreciated if I can compare my implementation with your – Ice Jun 05 '22 at 08:50
  • Here is what I used: https://drive.google.com/file/d/1QuJyBeSE4BrdnzTX-njVBmwdfDlTUCJf/view?usp=sharing – Asteroid Jun 05 '22 at 15:17
  • Hm, all work fine, and my code is the same, I feel that problem was inside the basic installation of CLLocationManager, because the first time, I has use a little more complex basic installation – Ice Jun 05 '22 at 19:04
0

The problem was solved by this answer and the help of Asteroid, appreciated your support. My code:

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)
        }
    }
}
Ice
  • 680
  • 1
  • 10
  • 23