(Step 1 and 2 below can be skipped if you only want to use iOS 6.)
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;
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>
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]];