Monday, 28 September 2015

How To Use Map Direction Find Rout In Map Direction Using MapKit In Ios/Iphone

==> Here to Finding Rout Direction Between Source And Destination :;

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

=> Here Some Prortocol confirm LIke MKMapVIewDelegate:
=> And Also Use CLLocationManager Used For Location:

@interface ViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
    CLLocationManager *clMngr;
    IBOutlet MKMapView *mpView;
   
    CLLocationCoordinate2D sourceCord;
    CLLocationCoordinate2D destCord;
   
    MKPinAnnotationView *pinAnnView;
}

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    // Keys to add in info.plist for iOS 8
==> Some Use d For UserSide Asking Permission Is Accessd Current Location:
   
//    NSLocationWhenInUseUsageDescription
//    NSLocationAlwaysUsageDescription
   
    clMngr = [[CLLocationManager alloc] init];
    clMngr.delegate = self;
    clMngr.desiredAccuracy = 1.0;
   
    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
    if ([clMngr respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [clMngr requestWhenInUseAuthorization];
    }
   
    [clMngr startUpdatingLocation];
   
    mpView.showsUserLocation = YES;

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

== Here some Used Source And Destination Cordinate And Location Of Cordinateing of Location Manger:
   
    sourceCord = clMngr.location.coordinate;
   
    destCord = CLLocationCoordinate2DMake(37.0000, -120.0000);
}

- (void)viewDidAppear:(BOOL)animated
{
    [self makeMapInCenter];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
==> Some MKMapView Delegate For Method And Action Used For Like MapView:


#pragma mark ----------------------
#pragma mark MKMapView Delegate Methods


- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView
{
    NSLog(@"%s",__FUNCTION__);
}

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
    NSLog(@"%s",__FUNCTION__);
   
    [self addDestinationAnnotation];
}

==> Used And Viewing Annotation In MapView For Displaying Anp Anotation:-

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"%s",__FUNCTION__);
   
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
   
    if (pinAnnView) {
        return pinAnnView;
    }
    ==> Here Set PinAnotationView for Falling Pin Current Or Destination Source In MKMapView:

    NSString *aStrId = @"destination";
    pinAnnView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:aStrId];
   
    if (!pinAnnView) {
        pinAnnView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:aStrId];
    }
   
//    pinAnnView.animatesDrop = YES;
    pinAnnView.canShowCallout = YES;
    pinAnnView.pinColor = MKPinAnnotationColorRed;
   
    return pinAnnView;
}


// For OverLay
==> For Overlay And Useing render For Overlay Map Route:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
    @try {
        if ([overlay isKindOfClass:[MKPolyline class]]) {
            MKPolylineRenderer *aPolyLineRenderer = [[MKPolylineRenderer alloc] initWithPolyline:(MKPolyline *)overlay];
           
            aPolyLineRenderer.strokeColor = [UIColor greenColor];
            aPolyLineRenderer.lineWidth = 5.0;
           
            return aPolyLineRenderer;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"Method :: %s  Exception :: %@",__FUNCTION__,exception.description);
    }
    @finally {
       
    }
}

==> Continuews Updted UserLocation in MKMapVIew:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [self getTheDirections];
}

==> CLLocation Manager Delegate Use For Accesing CLLocationManager And DidiUpdated User loction:-

#pragma mark ----------------------
#pragma mark Cllocation Manager Delegate Methods

// Location Manager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//    NSLog(@"%s",__FUNCTION__);

//    NSLog(@"%@", [locations lastObject]);
}

==> Some Method used For User Defin Method And to Make Map in Center Possition In Displaying In MkMapView:

#pragma mark ----------------------
#pragma mark User Define Methods

- (void)makeMapInCenter
{
    CLLocationCoordinate2D currentCord = clMngr.location.coordinate;
   
    MKCoordinateRegion reg = MKCoordinateRegionMakeWithDistance(currentCord, 10000, 10000);
   
    [mpView setRegion:reg];
   
}

- (void)addDestinationAnnotation
{
    @try {
        MKPointAnnotation *ann = [[MKPointAnnotation alloc] init];
        ann.coordinate = destCord;
        ann.title = @"California";
        ann.subtitle = @"Destination";
       
        [mpView addAnnotation:ann];
    }
    @catch (NSException *exception) {
        NSLog(@"Method :: %s  Exception :: %@",__FUNCTION__,exception.description);
    }
    @finally {
       
    }
}

// Get the directions for the mapView
=> Using This method and Action For Getting Direction of Source and Destination:

- (void)getTheDirections
{
    @try {
        MKPlacemark *aPlcSource = [[MKPlacemark alloc] initWithCoordinate:clMngr.location.coordinate addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];
       
        MKPlacemark *aPlcDest = [[MKPlacemark alloc] initWithCoordinate:destCord addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];
       
        MKMapItem *mpItemSource = [[MKMapItem alloc] initWithPlacemark:aPlcSource];
        [mpItemSource setName:@"Source"];
       
        MKMapItem *mpItemDest  = [[MKMapItem alloc] initWithPlacemark:aPlcDest];
        [mpItemDest setName:@"Dest"];
       
        MKDirectionsRequest *aDirectReq = [[MKDirectionsRequest alloc] init];
        [aDirectReq setSource:mpItemSource];
        [aDirectReq setDestination:mpItemDest];
        [aDirectReq setTransportType:MKDirectionsTransportTypeAutomobile];
       
        MKDirections *aDirections = [[MKDirections alloc] initWithRequest:aDirectReq];
       
        [aDirections calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
            if (error) {
                NSLog(@"Error :: %@",error);
            }
            else{
                NSArray *aArrRoutes = [response routes];
                NSLog(@"Routes :: %@",aArrRoutes);
               
                [mpView removeOverlays:mpView.overlays];
               
                [aArrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    MKRoute *aRoute = obj;
                   
                    [mpView addOverlay:aRoute.polyline];
                   
                    NSLog(@"Rout Name : %@",aRoute.name);
                    NSLog(@"Total Distance (in Meters) :%f",aRoute.distance);
                   
                    NSArray *aArrSteps = [aRoute steps];
                   
                    NSLog(@"Total Steps : %lu",(unsigned long)[aArrSteps count]);
                   
                    [aArrSteps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                        NSLog(@"Rout Instruction : %@",[obj instructions]);
                        NSLog(@"Rout Distance : %f",[obj distance]);
                    }];
                   
                }];
            }
           
        }];
       
    }
    @catch (NSException *exception) {
        NSLog(@"Method :: %s  Exception :: %@",__FUNCTION__,exception.description);
    }
    @finally {
       
    }
}
@end