将 Google 地图 URL 添加到当前位置
Append Google Maps URL with Current Location
我正在尝试使用 Core Location 和 MessageUI 使用 Google 地图 link 撰写电子邮件。目前我的代码生成这个字符串: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。非常感谢任何输入!
首先,您需要利用您在 info.plist
文件中添加的 NSLocationWhenInUseUsageDescription
和 NSLocationAlwaysUsageDescription
。
其次,您的 viewController 需要实施 <CLLocationManagerDelegate>
@interface ViewController ()<CLLocationManagerDelegate>
第三,在 viewDidLoad
方法中设置 locationManager
。
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
我正在尝试使用 Core Location 和 MessageUI 使用 Google 地图 link 撰写电子邮件。目前我的代码生成这个字符串: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。非常感谢任何输入!
首先,您需要利用您在 info.plist
文件中添加的 NSLocationWhenInUseUsageDescription
和 NSLocationAlwaysUsageDescription
。
其次,您的 viewController 需要实施 <CLLocationManagerDelegate>
@interface ViewController ()<CLLocationManagerDelegate>
第三,在 viewDidLoad
方法中设置 locationManager
。
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