ARCarMovement Library:

.h
#import 
#import 
#import 

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

#pragma mark - delegate protocol
@protocol ARCarMovementDelegate 


@optional
/**
* Tells the delegate that the specified marker will be work with animation.
*/
- (void)ARCarMovement:(GMSMarker *)movedMarker;

@end


@interface ARCarMovement : NSObject

#pragma mark - Public properties
/**
* The object that acts as the delegate of the ARCarMovement.
*/
@property (nonatomic, weak) id delegate;

/**
* assign the specified details to be work with animation for the Marker.
*/
- (void)ARCarMovement:(GMSMarker *)marker withOldCoordinate:(CLLocationCoordinate2D)oldCoordinate andNewCoordinate:(CLLocationCoordinate2D)newCoordinate inMapview:(GMSMapView *)mapView withBearing:(float)newBearing;

@end

.m

#import "ARCarMovement.h"

@implementation ARCarMovement

-(void)ARCarMovement:(GMSMarker *)marker withOldCoordinate:(CLLocationCoordinate2D)oldCoordinate andNewCoordinate:(CLLocationCoordinate2D)newCoordinate inMapview:(GMSMapView *)mapView withBearing:(float)newBearing {

dispatch_async(dispatch_get_main_queue(), ^{
//calculate the bearing value from old and new coordinates
//
float calBearing = [self getHeadingForDirectionFromCoordinate:oldCoordinate toCoordinate:newCoordinate]; //found bearing value by calculation
marker.groundAnchor = CGPointMake(0.5, 0.5);
marker.rotation = calBearing; //found bearing value by calculation when marker add
marker.position = oldCoordinate; //this can be old position to make car movement to new position

//marker movement animation
//
[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:5.0] forKey:kCATransactionAnimationDuration];
[CATransaction setCompletionBlock:^{

if (newBearing != 0) {
marker.rotation = newBearing; //New bearing value from backend after car movement is done
}
else {
marker.rotation = calBearing; //found bearing value by calculation old and new Coordinates
}

// delegate method pass value
//
if (self.delegate && [self.delegate respondsToSelector:@selector(ARCarMovement:)]) {
[self.delegate ARCarMovement:marker];
}
}];

marker.position = newCoordinate; //this can be new position after car moved from old position to new position with animation
marker.map = mapView;
marker.rotation = calBearing;
[CATransaction commit];

});
}

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

@end






Previous
Next Post »