iOS 10 heading arrow for MKUserLocation dot
I solved this by adding a subview to the MKUserLocation
annotationView, like so
func mapView(mapView: MKMapView, didAddAnnotationViews views: [MKAnnotationView]) {
if annotationView.annotation is MKUserLocation {
addHeadingViewToAnnotationView(annotationView)
}
}
func addHeadingViewToAnnotationView(annotationView: MKAnnotationView) {
if headingImageView == nil {
if let image = UIImage(named: "icon-location-heading-arrow") {
let headingImageView = UIImageView()
headingImageView.image = image
headingImageView.frame = CGRectMake((annotationView.frame.size.width - image.size.width)/2, (annotationView.frame.size.height - image.size.height)/2, image.size.width, image.size.height)
self.headingImageView = headingImageView
}
}
headingImageView?.removeFromSuperview()
if let headingImageView = headingImageView {
annotationView.insertSubview(headingImageView, atIndex: 0)
}
//use CoreLocation to monitor heading here, and rotate headingImageView as required
}
How to show in which side user turn phone, if I show user location using MKMapView
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)
}
}
}
Is the Current Location/Compass heading button available in the iOS sdk?
You need to create a MKUserTrackingBarButtonItem and pass it the MKMapview in the constructor, then add that button item to the navigation menu (or where ever it is your button should be).
- (void)viewDidLoad
{
[super viewDidLoad];
MKUserTrackingBarButtonItem *buttonItem = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.map];
self.navigationItem.rightBarButtonItem = buttonItem;
}
Related Topics
Change the Alpha Value of the Navigation Bar
Detect Which App Is Currently Running on iOS Using Sysctl
Uicollectionview Multiple Sections and Headers
Malloc Error "Can't Allocate Region" Failed with Error Code 12. Any Idea How to Resolve This
Obj-C Afnetworking 2.0 Post Request Does Not Work
Full Resolution Screenshots for iPhone 6 and 6+
How to Generate an Uiimage from Custom Text in Swift
How to Insert Cell in Uicollectionview Programmatically
How to View Testflight Crash Logs
How to Dynamically Format a Number to Have Commas in a Uitextfield Entry
Scanning Barcode or Qr Code in Swift 3.0 Using Avfoundation
Pop the Current View Using Segues/Storyboard on iOS 5
How to Check If Haptic Engine (Uifeedbackgenerator) Is Supported
Strong Reference to a Weak References Inside Blocks
How to Test for the Class of a Variable in Swift
Xcode: How to Change Layout of Views Between Landscape and Portrait Mode