注: - (集成
AppleMap
,不适用于 GoogleMap
)我做了以下事情:
我的需求:
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.
例如:如果船显示在用户位置位置并且它指向北方,但如果用户向西移动,那么船(用户位置引脚)也应该指向那个方向。
尝试使用以下代码:
//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()
}
调用
animateUserLocation
来自 didUpdateLocation
的方法方法但没有运气。请分享您的建议我做错了什么。提前致谢。
最佳答案
了解iOS's
位置概念完全有助于解决有关 AppleMap
的任何问题.
要移动 map 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
”将输出一个值,表示设备指向的方向。下一步是使用这些值并旋转您的
AnnotationView
或 Pin
因此。CGAffineTransformMakeRotation
可以帮助解决这个问题。例如,如果您想旋转到 imageview 以指向东北,这将需要 45 度值,您的代码可能看起来像这样。
float degrees = 45
imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180)
请注意
CGAffineTransformMakeRotation()
需要一个弧度值,在上面的示例中,我们通过将度数乘以半圆数将度数转换为弧度。以下链接真的很有帮助:
终于解决了我的问题。希望这个完整的答案也对其他人有所帮助。
关于ios - 在 iOS map 中更改 Pin 方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41198165/