Absolute Ripple
  • Absolute Ripple
  • Contact

Invoking the Map App to provide routing direction

18/10/2012

0 Comments

 
This post briefly describes how to invoke the Map app from your own app to provide routing direction from your current location to a particular destination coordinate.
(Step 1 and 2 below can be skipped if you only want to use iOS 6.)
Step 1: Include the CoreLocation framework
You need to first include the CoreLocation Framework in your project. Then you need import that framework in the relevant project class file:
#import <CoreLocation/CoreLocation.h>
Step 2: Add codes to obtain the current location
First, you need an object to hold the current location. In the .h file:
@property (nonatomic, strong) CLLocation *currentLocation;
And in the corresponding .m file:
@synthesize currentLocation = _currentLocation;
Next you need to make the class adopt to the CLLocationManagerDelegate protocol. So add <CLLocationManagerDelegate> to the @interface.

In viewDidLoad, insert:
if ([CLLocationManager locationServicesEnabled]) {
        //- initialise location manager
        CLLocationManager *locManager= [[CLLocationManager alloc] init];
        locManager.delegate = self;
        locManager.desiredAccuracy = kCLLocationAccuracyBest;  // modify to set desire accuracy
        locManager.distanceFilter = kCLDistanceFilterNone;   // modify if required     
        [locManager setPurpose:@"This app will use your location to determine your current position."];
        [locManager startUpdatingLocation];
    } else {
        //-- show an alert or whatever...;
    }

This first check if location service is available, then if it is, an instance of the location manager is created and start the location update.
Next, implement the following method in the CLLocationManagerDelegate protocol:
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    //- only use relatively recent event
    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (abs(howRecent) < 15.0) {
              newLocation.coordinate.latitude,
              newLocation.coordinate.longitude);
        self.currentLocation = newLocation;
    }
}

This method will be called once a location fix occurs. The coordinate is passed to the currentLocation object that you have created. The above code also provide a check to make sure that the location is not too old. If you don't need to do that, you can simply replace the above with:
self.currentLocation = newLocation;

Step 3: Include the CoreLocation framework
You need to first include the MapKit Framework in your project. Then you need import that framework in the relevant project class file:
#import <MapKit/MapKit.h>
Step 4: Codes to show invoke the Map App and Show the Route
Add the following method in your .m file:
- (void)showRouteInMapApp
{
    Class itemClass = [MKMapItem class];
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
        //- iOS 6
        CLLocationCoordinate2D destCoordinate = CLLocationCoordinate2DMake([_latitude doubleValue], [_longtitude doubleValue]);
        MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate: destCoordinate addressDictionary: nil];
        MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark: place];
        destination.name = @"NAME OF LOCATION...";
        NSArray* items = [[NSArray alloc] initWithObjects: destination, nil];
        NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys:
                                 MKLaunchOptionsDirectionsModeDriving,
                                 MKLaunchOptionsDirectionsModeKey, nil];
        [MKMapItem openMapsWithItems: items launchOptions: options];
    } else {
        //- use pre iOS 6
        //-- this opens in the map app
        NSString* url = [NSString stringWithFormat: @"maps://saddr=%f,%f&daddr=%f,%f",
                         _currentLocation.coordinate.latitude, _currentLocation.coordinate.longitude,[_latitude doubleValue],[_longtitude doubleValue]];
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; 
    }
}

The above first check to see if you are using iOS 6. If so, it uses the new MKMapItem approach, otherwise it uses an old approach to invoke the map app together with the current location coordinate determined via the codes in Step 1 and 2.
Instead of opening the map app, you can open Google map in Safari by replacing the above url with the following:
NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",
         _currentLocation.coordinate.latitude, _currentLocation.coordinate.longitude,[_latitude doubleValue],[_longtitude doubleValue]];

0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Archives

    August 2013
    July 2013
    June 2013
    May 2013
    January 2013
    October 2012
    September 2012
    March 2012
    February 2012

    Categories

    All
    Apple Policy
    In App Purchase
    Ios Programming
    Our Apps

    RSS Feed

    Disclaimers

    Here are some resources that I find useful in writing iOS apps. These are not necessarily unique as they are based on various sources online.
    This is mainly to provide a source of reference to myself and my associates but if it can be of use to someone else, that is good.
    Note that information is provided here as is. Use at your own risk. There is no guarantee that it is accurate. We will update the information as time and resources permit.

    Powered by Create your own unique website with customizable templates.