download ARCarMovement from Github
view.h
#import
#import "ARCarMovement.h"
@import GoogleMaps;
@interface animationViewController : UIViewController {
GMSMarker *driverMarker;
}
@property (strong, nonatomic) NSMutableArray *CoordinateArr;
@property (strong, nonatomic) GMSMapView *mapView;
@property (strong, nonatomic) ARCarMovement *moveMent;
@property CLLocationCoordinate2D oldCoordinate;
@property (weak, nonatomic) NSTimer *timer;
@property NSInteger counter;
view.m
#import "animationViewController.h"
@interface animationViewController (){
NSMutableArray *latArray,*longArray;
}
@end
@implementation animationViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.moveMent = [[ARCarMovement alloc]init];
self.moveMent.delegate = self;
//alloc array and load coordinate from json file
//
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"coordinates" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
self.CoordinateArr = [[NSMutableArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil]];
NSLog(@"%@",_CoordinateArr);
//set old coordinate
//
self.oldCoordinate = CLLocationCoordinate2DMake(17.4254355555556,78.4202311111111);
// Create a GMSCameraPosition that tells the map to display the marker
//
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:17.4254355555556
longitude:78.4202311111111
zoom:14];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.mapView.myLocationEnabled = YES;
self.mapView.delegate = self;
self.view = self.mapView;
// Creates a marker in the center of the map.
//
driverMarker = [[GMSMarker alloc] init];
driverMarker.position = self.oldCoordinate;
driverMarker.icon = [UIImage imageNamed:@"car.png"];
driverMarker.map = self.mapView;
//set counter value 0
//
self.counter = 0;
//start the timer, change the interval based on your requirement
//
self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerTriggered) userInfo:nil repeats:true];
}
#pragma mark - scheduledTimerWithTimeInterval Action
-(void)timerTriggered {
if (self.counter < latArray.count) {
// CLLocationCoordinate2D newCoordinate = CLLocationCoordinate2DMake([self.CoordinateArr[self.counter][@"lat"] floatValue],[self.CoordinateArr[self.counter][@"long"] floatValue]);
CLLocationCoordinate2D newCoordinate = CLLocationCoordinate2DMake(17.416587,78.432234);
// NSLog(@"%f,%f",[[latArray objectAtIndex:i]doubleValue],[[longArray objectAtIndex:i]doubleValue]);
[self.moveMent ARCarMovement:driverMarker withOldCoordinate:self.oldCoordinate andNewCoordinate:newCoordinate inMapview:self.mapView withBearing:0]; //instead value 0, pass latest bearing value from backend
self.oldCoordinate = newCoordinate;
self.counter = self.counter + 1;
}
// }
else {
[self.timer invalidate];
self.timer = nil;
}
}
#pragma mark - ARCarMovementDelegate
-(void)ARCarMovement:(GMSMarker *)movedMarker {
driverMarker = movedMarker;
driverMarker.map = self.mapView;
//animation to make car icon in center of the mapview
//
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:driverMarker.position zoom:15.0f];
[self.mapView animateWithCameraUpdate:updatedCamera];
}
@end
view.h
#import
#import "ARCarMovement.h"
@import GoogleMaps;
@interface animationViewController : UIViewController {
GMSMarker *driverMarker;
}
@property (strong, nonatomic) NSMutableArray *CoordinateArr;
@property (strong, nonatomic) GMSMapView *mapView;
@property (strong, nonatomic) ARCarMovement *moveMent;
@property CLLocationCoordinate2D oldCoordinate;
@property (weak, nonatomic) NSTimer *timer;
@property NSInteger counter;
view.m
#import "animationViewController.h"
@interface animationViewController (){
NSMutableArray *latArray,*longArray;
}
@end
@implementation animationViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.moveMent = [[ARCarMovement alloc]init];
self.moveMent.delegate = self;
//alloc array and load coordinate from json file
//
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"coordinates" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
self.CoordinateArr = [[NSMutableArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil]];
NSLog(@"%@",_CoordinateArr);
//set old coordinate
//
self.oldCoordinate = CLLocationCoordinate2DMake(17.4254355555556,78.4202311111111);
// Create a GMSCameraPosition that tells the map to display the marker
//
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:17.4254355555556
longitude:78.4202311111111
zoom:14];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.mapView.myLocationEnabled = YES;
self.mapView.delegate = self;
self.view = self.mapView;
// Creates a marker in the center of the map.
//
driverMarker = [[GMSMarker alloc] init];
driverMarker.position = self.oldCoordinate;
driverMarker.icon = [UIImage imageNamed:@"car.png"];
driverMarker.map = self.mapView;
//set counter value 0
//
self.counter = 0;
//start the timer, change the interval based on your requirement
//
self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerTriggered) userInfo:nil repeats:true];
}
#pragma mark - scheduledTimerWithTimeInterval Action
-(void)timerTriggered {
if (self.counter < latArray.count) {
// CLLocationCoordinate2D newCoordinate = CLLocationCoordinate2DMake([self.CoordinateArr[self.counter][@"lat"] floatValue],[self.CoordinateArr[self.counter][@"long"] floatValue]);
CLLocationCoordinate2D newCoordinate = CLLocationCoordinate2DMake(17.416587,78.432234);
// NSLog(@"%f,%f",[[latArray objectAtIndex:i]doubleValue],[[longArray objectAtIndex:i]doubleValue]);
[self.moveMent ARCarMovement:driverMarker withOldCoordinate:self.oldCoordinate andNewCoordinate:newCoordinate inMapview:self.mapView withBearing:0]; //instead value 0, pass latest bearing value from backend
self.oldCoordinate = newCoordinate;
self.counter = self.counter + 1;
}
// }
else {
[self.timer invalidate];
self.timer = nil;
}
}
#pragma mark - ARCarMovementDelegate
-(void)ARCarMovement:(GMSMarker *)movedMarker {
driverMarker = movedMarker;
driverMarker.map = self.mapView;
//animation to make car icon in center of the mapview
//
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:driverMarker.position zoom:15.0f];
[self.mapView animateWithCameraUpdate:updatedCamera];
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon