在这张图片中,GMSMapView
有两个标记
- 绿色标记是取件地点和
- 红色标记是放置位置标记
我想根据取件位置旋转地图视图,取件地点想从地图视图的左侧开始,放置位置想放在地图视图的右侧
旋转:使用委托方法或任何自定义方法GMSMapView
https://i.stack.imgur.com/Re5Mp.png
你可以这样尝试
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
在视图控制器中
func getHeadingForDirection(fromCoordinate fromLoc: CLLocationCoordinate2D, toCoordinate toLoc: CLLocationCoordinate2D) -> Float {
let fLat: Float = Float((self.mapView.camera.target.latitude).degreesToRadians)
let fLng: Float = Float((self.mapView.camera.target.longitude).degreesToRadians)
let tLat: Float = Float((fromLoc.latitude).degreesToRadians)
let tLng: Float = Float((fromLoc.longitude).degreesToRadians)
let degree: Float = (atan2(sin(tLng - fLng) * cos(tLat), cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(tLng - fLng))).radiansToDegrees
if degree >= 0 {
return degree
} else {
return 360 + degree
}
}
In mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition(
self.mapView.animate(toBearing: CLLocationDirection((self.getHeadingForDirection(fromCoordinate: self.pickUpMarker.position, toCoordinate: self.dropOffMarker.position) + 100) ))