Monday, 22 June 2015

Drawing Route - MKMapView for iOS

(or) 

Add the Frameworks : MapKit, CoreLocation

Add the Files : Place.h, Place.m, PlaceMark.h, PlaceMark.m, MapView.h, MapView.m, RegexKitLite.h, RegexKitLite.m


Add the linker flag in Project Settings : "Build Settings" -> "Linking" -> "Other Linker Flags" add the flag as "-licucore"

Set the Non ARC Flag for Files : Place.m, PlaceMark.m, MapView.m, RegexKitLite.m

Add the Lines in Info.Plist File : NSLocationAlwaysUsageDescription, NSLocationWhenInUseUsageDescription



MapViewController.h
#import <UIKit/UIKit.h>
#import "MapView.h"
#import "Place.h"

@interface MapViewController : UIViewController 
{

}

@end


MapViewController.m
- (void)viewDidLoad 
{
    [super viewDidLoad];

MapView* mapView = [[MapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    [self.view addSubview:mapView];
    
    Place* home = [[Place alloc] init];
    home.name = @"Home";
    home.description = @"Sweet home";
    home.latitude = 41.029598;
    home.longitude = 28.972985;
    
    Place* office = [[Place alloc] init];
    office.name = @"Office";
    office.description = @"Good office";
    office.latitude = 41.033586;
    office.longitude = 28.984546;
    
    [mapView showRouteFrom:home to:office];
}



-(void)getCurrentLocation
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    if(IS_OS_8_OR_LATER)
    {
        NSUInteger code = [CLLocationManager authorizationStatus];
        if (code == kCLAuthorizationStatusNotDetermined && ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]))
        {
            // choose one request according to your business.
            if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"])
            {
                [locationManager requestAlwaysAuthorization];
            }
            else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"])
            {
                [locationManager requestWhenInUseAuthorization];
            }
            else
            {
                NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
            }
        }
    }
    [locationManager startUpdatingLocation];
}


#pragma mark - CLLocationManagerDelegate
#pragma - didFailWithError
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
    
    
}

#pragma - didUpdateLocation
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    [locationManager stopUpdatingLocation];   // when u got the lat and long it stop update the location
    
    CLLocation *currentLocation = newLocation;
    
    if (currentLocation != nil)
    {
        getcurrlong = currentLocation.coordinate.longitude;
        getcurrlat = currentLocation.coordinate.latitude;
        
        NSLog(@"%f", getcurrlat);
        NSLog(@"%f", getcurrlong);
        
        [self Route];

// Reverse Geocoding - Resolving Address
    NSLog(@"Resolving the Address");
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            CLPlacemark *placemark = [placemarks lastObject];
            NSLog(@"%@",[NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@", placemark.subThoroughfare, placemark.thoroughfare, placemark.postalCode, placemark.locality, placemark.administrativeArea,placemark.country]);
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    }];
    }

}

No comments: