Search code examples
iosobjective-cgoogle-maps-markerscllocationmanagergmsmapview

Rotate GMSMarker in direction at which user facing


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 which have a fix location or angle at some location but i haven't fix location. Can anyone drive me at right direction.

I also used rotation property of the GMSMarker but it is not working.

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    if (newHeading.headingAccuracy < 0){
        NSLog(@"heading accuracy < 0");
        return;
    }

    // Convert Degree to Radian and move the needle
    float oldRad =  (- manager.heading.trueHeading) * M_PI / 180.0f;
    float newRad =  (- newHeading.trueHeading) * M_PI / 180.0f;

    // Compass animation
    CABasicAnimation *theAnimation;
    theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    theAnimation.fromValue = [NSNumber numberWithFloat:oldRad];
    theAnimation.toValue   = [NSNumber numberWithFloat:newRad];
    theAnimation.duration  = 0.5f;
    [source.layer addAnimation:theAnimation forKey:@"animateMyRotation"];

//    source.transform =  CGAffineTransformMakeRotation(newRad)
//    GMSMarker *source = [googleMap selectedMarker];
//    source.rotation = newRad;
}

Update: I have rotation method but is there any way to rotate the GMSMarker because there is no method for transform.

How uber rotate their car in google map?

enter image description here


Solution

  • 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);