0

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!

4

1 回答 1

0

首先,您需要利用您在文件中添加的NSLocationWhenInUseUsageDescriptionand 。NSLocationAlwaysUsageDescriptioninfo.plist

在此处输入图像描述

其次,您的 viewController 需要实现<CLLocationManagerDelegate>

@interface ViewController ()<CLLocationManagerDelegate>

第三,设置你locationManagerviewDidLoad方法。

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

四、实现-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations方法:

-(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);
}

最后,如果你在模拟器中测试,你需要模拟一个位置:

在此处输入图像描述

代码片段:https ://gist.github.com/ziyang0621/b1be760596da54873f81

于 2015-08-05T18:13:01.827 回答