我正在尝试使用Core Location和MessageUI撰写包含Google Maps链接的电子邮件。目前我的代码生成了这个字符串:https://maps.google.com?saddr=Current+Location&daddr=0.000000,0.000000。 我想用URL附加设备的经度和纬度。 这是我的实施:
#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];
}
我猜我没有正确实现locationManager。任何意见都非常感谢!
答案 0 :(得分:0)
首先,您需要在NSLocationWhenInUseUsageDescription
文件中添加NSLocationAlwaysUsageDescription
和info.plist
。
其次,你的viewController需要实现<CLLocationManagerDelegate>
@interface ViewController ()<CLLocationManagerDelegate>
第三,在locationManager
方法中设置viewDidLoad
。
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);
}
最后,如果你在模拟器中测试,你需要模拟一个位置: