更改 iOS 地图中的图钉方向

Change Pin direction in iOS Map

注意: -(集成 AppleMap,不适用于 GoogleMap

我做了以下事情:

  1. 实现了地图并将自定义图像添加到用户位置注释
  2. 当地图打开时,它会在正确的地方显示用户位置

我的要求:

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.

例如:如果船显示在用户位置并指向北方,但如果用户向西移动,则船(用户位置 Pin)也应指向该方向。

尝试使用以下代码:

//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()
    }

didUpdateLocation 方法中调用了 animateUserLocation 方法,但没有 Luck。

请分享您的建议,我做错了什么。提前致谢。

理解 iOS's 位置概念完全有助于解决有关 AppleMap 的任何问题。

要移动地图 Pin 或 AnnotationView ,我们可以使用 CoreLocation 框架输出与用户当前位置相关的数据。

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) } 
  }
}

在上面的例子中,“heading.magneticHeading”将输出一个表示设备指向方向的值。

  • 0 表示北方
  • 90 表示东
  • 180 表示南方
  • 270 表示西
  • 介于两者之间的其他一切

下一步是使用这些值并相应地轮换 AnnotationViewPin

CGAffineTransformMakeRotation 可以提供帮助。

例如,如果您想要旋转到 imageview 以指向东北,这将需要 45 度的值,您的代码可能看起来像这样。

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

请注意 CGAffineTransformMakeRotation() 需要一个弧度值,在上面的示例中,我们通过将度数乘以半圆的数量将度数转换为弧度。

以下链接非常有用:

终于解决了我的问题。希望这个完整的答案对其他人也有帮助。