Изменить направление булавки в iOS Map
Замечания : - (Интегрированный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()
}
НазываетсяanimateUserLocation
метод изdidUpdateLocation
Метод, но не удача.
Пожалуйста, поделитесь своим предложением, что я делаю неправильно. Заранее спасибо.