Change Pin direction in iOS Map

2019-02-16 05:33发布

  • SWIFT 3.0
  • MKMAPVIEW
  • iOS

Note : - (Integrated AppleMap ,Not working with GoogleMap)

I have done the following :

  1. Implemented map and Added custom Image to User Location Annotation
  2. When map open , it shows User Location at right Place

My Requirement :

When User Move into different direction staying at same place (or different place) the pin (at current location) should automatically point the direction in which user points.

E.g : If Boat is showing at User Location position and its pointing toward North but if user move toward West then boat (User Location Pin) also should point to that direction.

Tried with following Code :

//MARK:Change Direction Methods

    func angle(fromCoordinate first: CLLocationCoordinate2D, toCoordinate second: CLLocationCoordinate2D) -> Float {
        let deltaLongitude = second.longitude - first.longitude
        let deltaLatitude  = second.latitude - first.latitude
        let angle = (.pi * 0.5) - atan(deltaLatitude / deltaLongitude)
        if deltaLongitude > 0 {
            return Float(angle)
        }
        else if deltaLongitude < 0 {
            return Float(angle) + .pi
        }
        else if deltaLatitude < 0 {
            return .pi
        }

        return 0.0
    }

    //Animate direction of User Location
    func animateUserLocation() {

        //Old Coordinate (PLAT - Previous Lat , PLON - Previous Long) 
        let oldLocation = CLLocationCoordinate2D(latitude: UserDefaults.standard.value(forKey: "PLAT") as! CLLocationDegrees, longitude: UserDefaults.standard.value(forKey: "PLON") as! CLLocationDegrees)

        //New Coordinate (PLAT - Current Lat , PLON - Current Long)
        let newLocation = CLLocationCoordinate2D(latitude: UserDefaults.standard.value(forKey: "LAT") as! CLLocationDegrees, longitude: UserDefaults.standard.value(forKey: "LON") as! CLLocationDegrees)

        let getAngle = angle(fromCoordinate:oldLocation, toCoordinate:newLocation)
        var myAnnotation : RestaurantAnnotation?
        if annotationArray.count > 0{
            myAnnotation = annotationArray[0]
        }
        else {
            return
        }

        UIView.animate(withDuration: 2, animations: {() -> Void in
            myAnnotation?.coordinate = newLocation
            let annotationView = self.map.view(for: myAnnotation!)
            annotationView?.transform = CGAffineTransform(rotationAngle: CGFloat(getAngle))
        })

        //Save Previous lat long
        UserDefaults.standard.set(UserDefaults.standard.value(forKey: "LAT"), forKey: "PLAT")
        UserDefaults.standard.set(UserDefaults.standard.value(forKey: "LON"), forKey: "PLON")
        UserDefaults().synchronize()
    }

Called animateUserLocation method from didUpdateLocation Method but no Luck.

Kindly share your suggestion what i am doing wrong . Thanks in advance.

1条回答
叛逆
2楼-- · 2019-02-16 05:52

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 :

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

查看更多
登录 后发表回答