Change Pin direction in iOS Map

前端 未结 1 1680
渐次进展
渐次进展 2021-01-03 17:08
  • SWIFT 3.0
  • MKMAPVIEW
  • iOS

Note : - (Integrated AppleMap

1条回答
  •  隐瞒了意图╮
    2021-01-03 18:02

    Understanding iOS's location concepts completely helps to resolve any issue regarding AppleMap.

    To move map Pin or AnnotationView , we can use the CoreLocation framework which outputs data related to the users current location.

    import CoreLocation
    
    class ViewController: UIViewController, CLLocationManagerDelegate { 
      var locationManager:CLLocationManager! 
    
      override func viewDidLoad() {
        super.viewDidLoad() 
    
        locationManager  = CLLocationManager() 
        locationManager.delegate = self 
        locationManager.startUpdatingHeading() 
      } 
    
      func locationManager(manager: CLLocationManager!, didUpdateHeading heading: CLHeading!) {
        // This will print out the direction the device is heading
        println(heading.magneticHeading) } 
      }
    }
    

    In the above example, "heading.magneticHeading" will output a value representing the direction the device is pointed at.

    • 0 means north
    • 90 means east
    • 180 means south
    • 270 means west
    • everything else in between

    The next step is to use those values and rotate your AnnotationView or Pin accordingly.

    CGAffineTransformMakeRotation can help with this.

    For example if you want to rotate to imageview to point to northeast, which would require a degree value of 45, your code might look something like this.

    float degrees = 45 
    imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180)
    

    Just be aware that CGAffineTransformMakeRotation() expects a radian value, in the example above we've converted degrees to radians by multiplying degrees with the number of half circles.

    Following links really helpful :

    • https://stackoverflow.com/a/7634232/3400991

    Finally Resolved my issue. Hope this complete answer helps other too.

    0 讨论(0)
提交回复
热议问题