Want to continue displaying current location orientation in Swift MapKit

Asked 2 years ago, Updated 2 years ago, 83 views

I am studying code for iOS on Swift.
I was able to use MapKit to display the direction of the current location, but
When I zoom in, shrink, rotate, etc. on my iPhone, the orientation display disappears.
It doesn't show again, but I would appreciate it if you could tell me what to do.
(While trying various reference books and site connections, I still can't keep up with them.
I'm sorry.)
Is it correct that the set of userTrackingMode properties is written in this position?

import UIKit
    import MapKit

    classViewController:UIViewController, CLLocationManagerDelegate{

let manager = CLLocationManager()

@IBOutlet weak varmapView: MKMapView!
override func viewDidLoad(){
    super.viewDidLoad()
    manager.delegate=self

    ifCLLocationManager.authorizationStatus()!=.authorizedWhenInUse{
        manager.requestWhenInUseAuthorization()
    }

    // Follow the position
    mapView.userTrackingMode=MKUserTrackingMode.follow

    // Show current location orientation
    mapView.userTrackingMode=MKUserTrackingMode.followWithHeading

}
funclocationManager(_manager:CLLocationManager, didChangeAuthorization status:CLAuthorizationStatus){
    if status==.authorizedWhenInUse{
        manager.requestLocation()
    }
}
funclocationManager(_manager:CLLocationManager, didUpdateLocations:[CLLocation]){
    print("locations:\(locations)")
    let current=locations[0]
    let region = MKCoordinateRegionMakeWithDistance(current.coordinate,500,500);
    mapView.setRegion(region, animated:true)


}
US>funclocationManager(_manager:CLLocationManager, didFailWithError:Error) {
    print("error=\(error)")
}

override funcdidReceiveMemoryWarning(){
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
    }

swift ios mapkit

2022-09-30 19:35

1 Answers

Have you already solved this problem?

As mentioned in shu223's article, if a user moves the map even a little, it is
Return to MKUserTrackingModeNone.In other words, the orientation disappears in the specification.

If you look at the standard map application, you can see that if you move the map a little bit, MKUserTrackingModeFollow or MKUserTrackingModeFollowWithHeading, you will be forced back to MKUserTrackingModeNone.

userTrackingModeThe set of properties is fine at that location, but

mapView.userTrackingMode=MKUserTrackingMode.follow

and

mapView.userTrackingMode=MKUserTrackingMode.followWithHeading

One or the other is sufficient.If you want to show the direction, erase the former, and I think the latter will work without any problems.


2022-09-30 19:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.