objective-cgoogle-mapsxcode6core-locationmessageui

Append Google Maps URL with Current Location


I am trying to compose an email with a Google Maps link using Core Location and MessageUI. Presently my code produces this string: https://maps.google.com?saddr=Current+Location&daddr=0.000000,0.000000. I would like to append the device's longitude and latitude with the URL. Here is my implementation:

#import "ViewController.h"
@interface ViewController () <CLLocationManagerDelegate>
@end

@implementation ViewController{
NSString *currentLongitude;
NSString *currentLatitude;
NSString *googleMapsURL;
CLLocationManager *locationManager_;
}

- (void)viewDidLoad {
[super viewDidLoad];

locationManager_ = [[CLLocationManager alloc] init];
locationManager_.delegate = self;
locationManager_.distanceFilter = kCLDistanceFilterNone;
locationManager_.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager_ requestAlwaysAuthorization];
[locationManager_ startUpdatingLocation];
}

- (IBAction)composeMailButton:(id)sender {

NSString *bodyHeader = @"Here are you directions:";
NSString *mailBody = [NSString stringWithFormat:@"%@\n%@", bodyHeader, googleMapsURL];

MFMailComposeViewController *emailComposer = [[MFMailComposeViewController alloc] init];

[emailComposer setSubject:@"Google Maps Directions"];
[emailComposer setMessageBody:mailBody isHTML:NO];
[emailComposer setToRecipients:@[@"castro.michael87@gmail.com"]];
[emailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

[self presentViewController:emailComposer animated:YES completion:nil];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *newLocation = [locations lastObject];
NSString *googleMapsURL = [[NSString alloc] initWithFormat:@"https://maps.google.com?saddr=Current+Location&daddr=%1.6f,%1.6f",newLocation.coordinate.latitude, newLocation.coordinate.longitude];
}

I am guessing that I have not correctly implemented the locationManager. Any input is very much appreciated!


Solution

  • First, you need to make use that you added the NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription in your info.plist file.

    enter image description here

    Second, your viewController need to implement the <CLLocationManagerDelegate>

    @interface ViewController ()<CLLocationManagerDelegate>
    

    Third, setup your locationManager in your viewDidLoad method.

        locationManager_ = [[CLLocationManager alloc] init];
        locationManager_.delegate = self;
        locationManager_.distanceFilter = kCLDistanceFilterNone;
        locationManager_.desiredAccuracy = kCLLocationAccuracyBest;
        [locationManager_ requestAlwaysAuthorization];
        [locationManager_ startUpdatingLocation];
    

    Fourth, implement the -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations method:

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
        CLLocation *newLocation = [locations lastObject];
        NSString *googleMapsURL = [[NSString alloc] initWithFormat:@"https://maps.google.com?saddr=Current+Location&daddr=%1.6f,%1.6f",newLocation.coordinate.latitude, newLocation.coordinate.longitude];
        NSLog(@"%@", googleMapsURL);
    }
    

    Finally, if you test in simulator, you need to simulate a location:

    enter image description here

    Code Snippet: https://gist.github.com/ziyang0621/b1be760596da54873f81