Drive Link :
https://drive.google.com/open?id=1WYxFZO3xHxTU6W4Yq5qSmsSDxPftBzfJ
Libraries Used:
ARCarMovement
GoogleMapsSDk
https://drive.google.com/open?id=1WYxFZO3xHxTU6W4Yq5qSmsSDxPftBzfJ
Libraries Used:
ARCarMovement
GoogleMapsSDk
#import "AppDelegate.h"
@import GoogleMaps;
@import GooglePlaces;
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GMSServices provideAPIKey:@"AIzaSyC95JDWt9Y_WSIEWlXzccsDo_7-b8IYpO4"];
[GMSServices provideAPIKey:@"AIzaSyC95JDWt9Y_WSIEWlXzccsDo_7-b8IYpO4"];
[GMSPlacesClient provideAPIKey:@"AIzaSyC95JDWt9Y_WSIEWlXzccsDo_7-b8IYpO4"];
[GMSServices provideAPIKey:@"AIzaSyC95JDWt9Y_WSIEWlXzccsDo_7-b8IYpO4"];
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
//***ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *searchBtn;
- (IBAction)onTappingSearchBtn:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *pickupLocationTF;
@property (weak, nonatomic) IBOutlet UITextField *dropingLocationTF;
@end
//***viewController.m
#import "ViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#import "ARCarMovement.h"
@import GoogleMaps;
@import GooglePlaces;
@import GooglePlacePicker;
@interface ViewController ()<GMSMapViewDelegate,GMSPlacePickerViewControllerDelegate,GMSAutocompleteViewControllerDelegate,UITextFieldDelegate,CLLocationManagerDelegate,ARCarMovementDelegate>
@property (strong, nonatomic) IBOutlet GMSMapView *mapView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
//ARCarmovement
@property (strong, nonatomic) NSMutableArray *CoordinateArr;
@property (strong, nonatomic) ARCarMovement *moveMent;
@property CLLocationCoordinate2D oldCoordinate;
@property CLLocationCoordinate2D newCoordinate;
@property (weak, nonatomic) NSTimer *timer;
@property NSInteger counter;
@end
@implementation ViewController
{
GMSMarker *souceLocation;
GMSMarker *DesitnationLocation;
UIImageView *souceLocationView;
UIImageView *destinationLocationView;
GMSPlacesClient *_placesClient;
CLLocationManager *locationManager;
GMSPlacePickerViewController *_placePicker;
GMSMutablePath *path;
GMSMutablePath *path1;
GMSPolyline *rectangle;
NSString *placeName,*placeAddress;
GMSAutocompleteViewController *acController;
UITextField *tappedtextField;
NSMutableArray *latArray,*longArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
path = [GMSMutablePath path];
latArray=[[NSMutableArray alloc]init];
self.pickupLocationTF.delegate=self;
self.dropingLocationTF.delegate=self;
acController = [[GMSAutocompleteViewController alloc] init];
acController.delegate = self;
[GMSServices openSourceLicenseInfo];
self.moveMent = [[ARCarMovement alloc]init];
self.moveMent.delegate = self;
//*******To get current location
/*
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:CLLocationCoordinate2DMake(0, 0) zoom: 10];
_mapView = [GMSMapView mapWithFrame:CGRectMake(0, 250, self.view.bounds.size.width,650) camera:camera];
_mapView.myLocationEnabled = YES;
[self.view addSubview:_mapView];
[locationManager startUpdatingLocation];
*/
NSString *lat=@"51.5";
NSString *longi=@"-0.127";
NSString *place=@"HYDERABAD";
[locationManager requestAlwaysAuthorization];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[lat floatValue] longitude:[longi floatValue] zoom:14];
_mapView = [GMSMapView mapWithFrame:CGRectMake(0, 250, self.view.bounds.size.width,650) camera:camera];
[self.view addSubview: _mapView];
_mapView.delegate = self;
//***adding image to marker***
// UIImage *house = [UIImage imageNamed:@"icon.jpg"];
// house = [house imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
// souceLocationView = [[UIImageView alloc] initWithImage:house];
//***adding Marker
souceLocationView.tintColor = [UIColor redColor];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
souceLocation = [GMSMarker markerWithPosition:position];
souceLocation.title = @"Home";
souceLocation.iconView = souceLocationView;
souceLocation.tracksViewChanges = YES;
souceLocation.icon = [UIImage imageNamed:@"car.png"];
souceLocation.map = self.mapView;
self.counter = 0;
// souceLocation.flat = YES;
// souceLocation.groundAnchor = CGPointMake(0.5, 0.5);
souceLocation.snippet = place;
souceLocation.tracksInfoWindowChanges = YES;
souceLocation.infoWindowAnchor = CGPointMake(0.5, 0.5);
souceLocation.draggable=YES;
//gmsPlaces
_placesClient = [GMSPlacesClient sharedClient];
}
//Update current location
/*
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude
longitude:newLocation.coordinate.longitude
zoom:17.0];
[_mapView animateToCameraPosition:camera];
}
*/
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if (self.pickupLocationTF==textField) {
tappedtextField=_pickupLocationTF;
[self presentViewController:acController animated:YES completion:nil];
}else if (self.dropingLocationTF==textField) {
tappedtextField=_dropingLocationTF;
[self presentViewController:acController animated:YES completion:nil];
}
}
- (void)viewController:(GMSAutocompleteViewController *)viewController
didAutocompleteWithPlace:(GMSPlace *)place {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:place.coordinate.latitude longitude:place.coordinate.longitude zoom:14];
_mapView = [GMSMapView mapWithFrame:CGRectMake(0, 250, self.view.bounds.size.width,650) camera:camera];
_mapView.myLocationEnabled = YES;
[_mapView setMapType:kGMSTypeNormal];
[self.view addSubview:_mapView];
[self dismissViewControllerAnimated:YES completion:nil];
if (tappedtextField==_pickupLocationTF) {
NSLog(@"Place name %@", place.name);
NSLog(@"Place address %@", place.formattedAddress);
NSLog(@"Place attributions %@", place.attributions.string);
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude);
souceLocation = [GMSMarker markerWithPosition:position];
souceLocation.title=place.name;
self.pickupLocationTF.text=place.name;
souceLocation.snippet=place.formattedAddress;
placeAddress=place.formattedAddress;
NSLog(@"%@ %@",souceLocation.title,souceLocation.snippet);
souceLocation.flat = YES;
souceLocation.groundAnchor = CGPointMake(0.5, 0.5);
souceLocation.map = _mapView;
path1 = [GMSMutablePath path];
[path1 addCoordinate:place.coordinate];
NSString * latPoint = [NSString stringWithFormat:@"%f", place.coordinate.latitude];
NSString *longPoint = [NSString stringWithFormat:@"%f", place.coordinate.longitude];
[path1 addCoordinate:CLLocationCoordinate2DMake(latPoint.doubleValue,longPoint.doubleValue)];
self.oldCoordinate=CLLocationCoordinate2DMake(latPoint.doubleValue,longPoint.doubleValue);
souceLocation.position=self.oldCoordinate;
}
else if (_dropingLocationTF==tappedtextField)
{
DesitnationLocation.tracksViewChanges = YES;
DesitnationLocation.map = self.mapView;
// souceLocation.flat = YES;
// souceLocation.groundAnchor = CGPointMake(0.5, 0.5);
//souceLocation.snippet = place;
DesitnationLocation.tracksInfoWindowChanges = YES;
DesitnationLocation.infoWindowAnchor = CGPointMake(0.5, 0.5);
DesitnationLocation.draggable=YES;
NSLog(@"Place name %@", place.name);
NSLog(@"Place address %@", place.formattedAddress);
NSLog(@"Place attributions %@", place.attributions.string);
destinationLocationView.tintColor = [UIColor redColor];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude);
self.newCoordinate=CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude);
DesitnationLocation = [GMSMarker markerWithPosition:position];
DesitnationLocation.title=place.name;
DesitnationLocation.iconView = destinationLocationView;
self.dropingLocationTF.text=place.name;
DesitnationLocation.snippet=place.formattedAddress;
placeAddress=place.formattedAddress;
path=path1;
}
[self polyline];
}
-(void)polyline
{
[path removeAllCoordinates];
[_mapView clear];
if ([_pickupLocationTF hasText] && [_dropingLocationTF hasText])
{
souceLocation.icon = [UIImage imageNamed:@"car.png"];
souceLocation.map = _mapView;
DesitnationLocation.map = _mapView;
NSString *str = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&key=AIzaSyBkjmHAHytmX3iftucKIeqJn4qMEIjhkFo",souceLocation.title,DesitnationLocation.title];
NSURL *url=[[NSURL alloc]initWithString:[str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(data == nil) {
return;
}else
{
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray* latestRoutes = [json objectForKey:@"routes"];
NSString *points=[[[latestRoutes objectAtIndex:0] objectForKey:@"overview_polyline"] objectForKey:@"points"];
@try {
// TODO: better parsing. Regular expression?
NSArray *temp= [self decodePolyLine:[points mutableCopy]];
for(int idx = 0; idx < [temp count]; idx++){
CLLocation *location=[temp objectAtIndex:idx];
[path addCoordinate:location.coordinate];
NSString * latPoint = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
NSString *longPoint = [NSString stringWithFormat:@"%f", location.coordinate.longitude];
NSLog(@"lat is %@ : lon is %@",latPoint, longPoint);
[path addCoordinate:CLLocationCoordinate2DMake(latPoint.doubleValue,longPoint.doubleValue)];
NSLog(@"%f",latPoint.doubleValue);
[latArray addObject:latPoint];
}
}
@catch (NSException * e)
{
// TODO: show erro
}
}
[self pathMakingPolyline];
}];
[dataTask resume];
}
}
-(void)pathMakingPolyline
{
dispatch_async(dispatch_get_main_queue(), ^{
rectangle.map=nil;
rectangle = [GMSPolyline polylineWithPath:path];
rectangle.strokeWidth = 2.f;
rectangle.map = _mapView;
[self.view addSubview: _mapView];
self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerTriggered) userInfo:nil repeats:true];
});
}
//****Decodingpolyline
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init] ;
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5] ;
NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5] ;
printf("[%f,", [latitude doubleValue]);
printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] ;
[array addObject:loc];
}
return array;
}
- (void)mapView:(GMSMapView *)mapView
idleAtCameraPosition:(GMSCameraPosition *)position {
[UIView animateWithDuration:5.0
animations:^{
souceLocationView.tintColor = [UIColor blueColor];
destinationLocationView.tintColor = [UIColor blueColor];
}
completion:^(BOOL finished) {
// Stop tracking view changes to allow CPU to idle.
souceLocation.tracksViewChanges = NO;
DesitnationLocation.tracksViewChanges = NO;
}];
}
// Add a UIButton in Interface Builder, and connect the action to this function.
- (IBAction)getCurrentPlace:(UIButton *)sender {
self.nameLabel.text = souceLocation.title;
self.addressLabel.text = souceLocation.snippet;
}
- (IBAction)pickPlace:(UIButton *)sender
{
GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:nil];
GMSPlacePickerViewController *placePicker =
[[GMSPlacePickerViewController alloc] initWithConfig:config];
placePicker.delegate = self;
[self presentViewController:placePicker animated:YES completion:nil];
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(-33.865143, 151.2099);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(center.latitude + 0.001,
center.longitude + 0.001);
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(center.latitude - 0.001,
center.longitude - 0.001);
GMSCoordinateBounds *viewport = [[GMSCoordinateBounds alloc] initWithCoordinate:northEast coordinate:southWest];
GMSPlacePickerConfig *config1 = [[GMSPlacePickerConfig alloc] initWithViewport:viewport];
}
// To receive the results from the place picker 'self' will need to conform to
// GMSPlacePickerViewControllerDelegate and implement this code.
- (void)placePicker:(GMSPlacePickerViewController *)viewController didPickPlace:(GMSPlace *)place {
// Dismiss the place picker, as it cannot dismiss itself.
[viewController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"Place name %@", place.name);
NSLog(@"Place address %@", place.formattedAddress);
NSLog(@"Place attributions %@", place.attributions.string);
placeName=place.name;
}
- (void)placePickerDidCancel:(GMSPlacePickerViewController *)viewController {
// Dismiss the place picker, as it cannot dismiss itself.
[viewController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"No place selected");
}
- (IBAction)onTappingSearchBtn:(id)sender
{
[self presentViewController:acController animated:YES completion:nil];
}
- (void)viewController:(GMSAutocompleteViewController *)viewController
didFailAutocompleteWithError:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:nil];
// TODO: handle the error.
NSLog(@"Error: %@", [error description]);
}
// User canceled the operation.
- (void)wasCancelled:(GMSAutocompleteViewController *)viewController {
[self dismissViewControllerAnimated:YES completion:nil];
}
// Turn the network activity indicator on and off again.
- (void)didRequestAutocompletePredictions:(GMSAutocompleteViewController *)viewController {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)didUpdateAutocompletePredictions:(GMSAutocompleteViewController *)viewController {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
#pragma mark - scheduledTimerWithTimeInterval Action
-(void)timerTriggered {
if (self.counter < latArray.count)
{
[self.moveMent ARCarMovement:souceLocation withOldCoordinate:self.oldCoordinate andNewCoordinate:self.newCoordinate inMapview:self.mapView withBearing:0];
//instead value 0, pass latest bearing value from backend
self.oldCoordinate = self.newCoordinate;
self.counter = self.counter + 1;
}
// }
else {
[self.timer invalidate];
self.timer = nil;
}
}
-(void)ARCarMovement:(GMSMarker *)movedMarker {
souceLocation = movedMarker;
souceLocation.map = self.mapView;
//animation to make car icon in center of the mapview
//
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:souceLocation.position zoom:15.0f];
[self.mapView animateWithCameraUpdate:updatedCamera];
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon