使用当前位置附加Google地图网址

时间:2015-08-04 23:39:31

标签: objective-c google-maps xcode6 core-location messageui

我正在尝试使用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。任何意见都非常感谢!

1 个答案:

答案 0 :(得分:0)

首先,您需要在NSLocationWhenInUseUsageDescription文件中添加NSLocationAlwaysUsageDescriptioninfo.plist

enter image description here

其次,你的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);
}

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

enter image description here

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