我正在实现一个地图MapView,该地图包含一个自定义移动注解,其标题与其他位置跟踪应用程序一样。 我已经找到了this,this,this的几个质量检查清单,但显然我面临着不同的问题。 问题是“航向”始终返回度,如90,270,0 ...如下所示,因此注释无法向正确的方向旋转。
Heading Optional(270.0000855664996)
Latitude 37.33761393
Heading Optional(0.0)
Latitude 37.33762895
Heading Optional(270.0000898818526)
Latitude 37.33762895
Heading Optional(0.0)
Latitude 37.33763576
Heading Optional(270.0000792436673)
Latitude 37.33763576
Heading Optional(180.0)
Latitude 37.33762658
Heading Optional(270.000079744022)
Latitude 37.33762658
Heading Optional(180.0)
Latitude 37.33761465
Heading Optional(270.00007860679307)
Latitude 37.33761465
Heading Optional(0.0)
Latitude 37.33762383
Heading Optional(270.0000852056452)
这是我使用MapKit的简化代码:
if let latitude = self.DriverLatitude , let longitude = self.DriverLongitude {
let DriverCoordinate = CLLocationCoordinate2D(latitude: latitude
, longitude: longitude)
self.DriverLocation = CLLocation(latitude: latitude
, longitude: longitude)
if self.DriverPriorLocation == nil {
self.DriverPriorLocation = self.DriverLocation
}else{
if let DriverPriorLocation = self.DriverPriorLocation ,
let DriverLocation = self.DriverLocation {
self.DriverHeading = DriverLocation
.bearingRadianTo(location: DriverPriorLocation)
}
self.DriverPriorLocation = self.DriverLocation
}
if !self.isDriverAnnotationPinned {
self.Map.addAnnotation(self.DriverAnnotation)
self.isDriverAnnotationPinned = true
} else {
//self.Map.addAnnotation(self.DriverAnnotation)
if let DriverAnnotationView = self.DriverAnnotationView {
DriverAnnotationView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
if let DriverHeading = self.DriverHeading {
if DriverHeading != 0 {
UIView.animate(withDuration: 5, animations: {
self.DriverAnnotation.coordinate = DriverCoordinate
DriverAnnotationView.transform =
CGAffineTransform(rotationAngle: CGFloat(DriverHeading))
}, completion: nil)
print("HeadingAngle",DriverHeading)
}
}
}
}
self.Map.showAnnotations(self.Map.annotations, animated: true)
}
航向计算为:
extension CLLocation {
func getRadiansFrom(degrees: Double ) -> Double {
return degrees * .pi / 180
}
func getDegreesFrom(radians: Double) -> Double {
return radians * 180 / .pi
}
func bearingRadianTo(location: CLLocation) -> Double {
let lat1 = self.getRadiansFrom(degrees: self.coordinate.latitude)
let lon1 = self.getRadiansFrom(degrees: self.coordinate.longitude)
let lat2 = self.getRadiansFrom(degrees: location.coordinate.latitude)
let lon2 = self.getRadiansFrom(degrees: location.coordinate.longitude)
let dLon = lon2 - lon1
let y = sin(dLon) * cos(lat2)
let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon)
var radiansBearing = atan2(y, x)
if radiansBearing < 0.0 {
radiansBearing += 2 * .pi
}
return radiansBearing
}
func bearingDegreesTo(location: CLLocation) -> Double {
return self.getDegreesFrom(radians: self.bearingRadianTo(location: location))
}
}
我也尝试使用GoogleMap来实现这一点,认为可能是MapKit问题,这是Google Map的代码:
if let Latitude = self.DriverLatitude, let Longitude = self.DriverLongitude {
self.DriverLocation = CLLocationCoordinate2D(latitude: Latitude, longitude: Longitude)
if self.DriverPriorLocation == nil {
self.DriverPriorLatitude = Latitude
self.DriverPriorLongitude = Longitude
self.DriverPriorLocation = CLLocationCoordinate2D(latitude: Latitude, longitude: Longitude)
} else {
self.DriverHeading = GMSGeometryHeading(self.DriverPriorLocation, self.DriverLocation)
print("Heading",self.DriverHeading)
}
print("Latitude",Latitude)
CATransaction.begin()
CATransaction.setAnimationDuration(2.0)
self.MapView.moveCamera(GMSCameraUpdate.setTarget(self.DriverLocation))
if let Heading = self.DriverHeading {
self.Marker.rotation = Heading
}
self.Marker.position = self.DriverLocation
CATransaction.commit()
self.DriverPriorLatitude = Latitude
self.DriverPriorLongitude = Longitude
self.DriverPriorLocation = self.DriverLocation
}