更改iOS地图中的Pin方向
- SWIFT 3.0
- 的MKMapView
- iOS版
注意: – (集成AppleMap
,不适用于GoogleMap
)
我做了以下几件事:
- 实施地图和添加自定义图像到用户位置注释
- 当地图打开时,它显示用户位置在正确的地方
我的要求:
当用户移动到不同的方向停留在同一地点(或不同的地方)时,引脚(当前位置)应该自动指向用户指向的方向。
例如:如果船在用户位置位置显示并且其指向北方,但是如果用户移向西方,则船(用户位置引脚)也应该指向该方向。
用下面的代码试过:
//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
方法,但没有运气。
请分享你的build议,我做错了什么。 提前致谢。
了解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代表西方
- 一切之间的一切
下一步是使用这些值并相应地旋转您的AnnotationView
或Pin
。
CGAffineTransformMakeRotation
可以帮助这一点。
例如,如果您要旋转到imageview指向东北,这将需要度数值为45,您的代码可能看起来像这样。
float degrees = 45 imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180)
请注意, CGAffineTransformMakeRotation()
需要一个弧度值,在上面的示例中,我们已经通过将度数乘以半个圆圈数将度数转换为弧度。
以下链接确实有帮助:
终于解决了我的问题。 希望这个完整答案也能帮助其他人。