I have requirement like on my current location one view will display. It will rotate if device was rotate or location will be change.I research lot but got all the code whic
Try this, it worked for me
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading:CLHeading) {
UIView.animate(withDuration: 0.005) {
let angle = newHeading.trueHeading.toRadians() // convert from degrees to radians
self.yourImageHere.transform = CGAffineTransform(rotationAngle: CGFloat(angle)) // rotate the picture
}
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.startUpdatingHeading()
}
marker.rotation = course_of_location;
Note: rotation to pin will happen only during movement. in case of static device there will be -1 value of location.course. Also this have no relation with device orientation. If you are looking to move pin according to device heading then do this
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
marker.rotation = (manager.heading.trueHeading) * M_PI / 180.0f; }
We can rotate image based on course property CLLocation Class
let marker:GMSMarker = GMSMarker.init(position: currentLocation!)
let head = locationManager.location?.course ?? 0
marker.rotation = head
marker.icon = UIImage(named: "testyCar.png")
marker.map = mapView
//just do this only
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
double heading = newHeading.trueHeading;
marker.groundAnchor = CGPointMake(0.5, 0.5);
marker.rotation = heading;
marker.map = mapView;
}
Swift 4+
Thanks to priya.vr. When you updating location marker, try this:
let locationManager = CLLocationManager()
marker.position = position
marker.appearAnimation = .none
marker.rotation = locationManager.location?.course ?? 0
marker.map = map
you can do something like -
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
CLLocationDirection direction = newHeading.trueHeading;
lastDriverAngleFromNorth = direction;
self.driverMarker.rotation = lastDriverAngleFromNorth - mapBearing;
}
#pragma mark - GMSMapViewDelegate
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
mapBearing = position.bearing;
self.driverMarker.rotation = lastDriverAngleFromNorth - mapBearing;
}