Аннотация Заголовок Проблема - PullRequest
0 голосов
/ 28 февраля 2019

Я внедряю Map MapView, включая пользовательскую аннотацию перемещения с заголовком, как в других приложениях отслеживания местоположения.Я нашел пару QA об этом это , это , это , но, очевидно, я столкнулся с другой проблемой.Проблема в том, что заголовок всегда возвращает градусы, такие как 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
}
...