Rotate GMSMarker in direction at which user facing

chirag shah picture chirag shah · Apr 6, 2016 · Viewed 11.8k times · Source

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

Answer

priya.vr picture priya.vr · Jan 5, 2017

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