Rotate GMSMarker in direction at which user facing

落花浮王杯 提交于 2019-11-30 03:28:59
Sunil_Vaishnav

Current location 'CLLocation' object has a property called 'course'

@property(readonly, nonatomic) CLLocationDirection course;

of type CLLocationDirection(typedef of double) which is an angle of the location.

For car to rotate, You need extra field in your backend, direction, along with latitude and longitude. Use this information to rotate car by applying Transform on UIView

CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);

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 

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;
}
ios meridian

//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;
}
BizDev

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; }

Here the code modified with the changes suggested by Oren Trutner and from me:

You just need to pass old location and new location. By passing required data you will get rotation value which is in float.

#define degreesToRadians(x) (M_PI * x / 180.0)
#define radiansToDegrees(x) (x * 180.0 / M_PI)

- (float)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
    float fLat = degreesToRadians(fromLoc.latitude);
    float fLng = degreesToRadians(fromLoc.longitude);
    float tLat = degreesToRadians(toLoc.latitude);
    float tLng = degreesToRadians(toLoc.longitude);

    float degree = radiansToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)));

    if (degree >= 0) {
        return degree;
    } else {
        return 360+degree;
    }
}

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!