我喜欢我的应用程序以与Apple的地图在screenShot中相同的方式发送用户位置。我读过这个答案“link”,但我不明白如何实现它。我已经尝试使用CLPlaceMark,但我只能以文本格式发送地址。这是我的代码。
//.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate, MFMessageComposeViewControllerDelegate>{
CLLocationCoordinate2D coordinates;
MKMapView *mapView;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) CLLocationManager *locationManager;
-(IBAction)SetMap:(id)sender;
- (IBAction)getLocation:(id)sender;
- (IBAction)send:(id)sender;
@end
//.m
#import "ViewController.h"
#import <MessageUI/MFMessageComposeViewController.h>
@interface ViewController ()
@end
@implementation ViewController{
CLGeocoder *geoCoder;
CLPlacemark *placeMark;
}
@synthesize mapView;
@synthesize locationManager;
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
mapView.showsUserLocation = YES;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
[locationManager startUpdatingLocation];
CLLocation *location1 = [locationManager location];
coordinates = [location1 coordinate];
geoCoder = [[CLGeocoder alloc]init];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Location: %@", newLocation);
CLLocation *currentLocation = newLocation;
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] > 0) {
placeMark = [placemarks lastObject];
_adressLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placeMark.subThoroughfare, placeMark.thoroughfare,
placeMark.postalCode, placeMark.locality,
placeMark.administrativeArea,
placeMark.country];
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)SetMap:(id)sender{
switch (((UISegmentedControl *)sender).selectedSegmentIndex) {
case 0:
mapView.mapType = MKMapTypeStandard;
break;
case 1:
mapView.mapType = MKMapTypeSatellite;
break;
case 2:
mapView.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
- (IBAction)getLocation:(id)sender {
mapView.showsUserLocation = YES;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
coordinates = [location coordinate];
}
- (IBAction)send:(id)sender {
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
[controller setMessageComposeDelegate:self];
if([MFMessageComposeViewController canSendText])
{
[controller setRecipients:[NSArray arrayWithObjects:nil]];
[controller setBody:[NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placeMark.subThoroughfare, placeMark.thoroughfare,
placeMark.postalCode, placeMark.locality,
placeMark.administrativeArea,
placeMark.country]];
[self presentViewController:controller animated:YES completion:NULL];
}else{
NSLog(@"NO Texting");
}
}
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
[self dismissViewControllerAnimated:YES completion:NULL];
}
@end
答案 0 :(得分:2)
现在你把它“发送”给用户是什么意思?如果您只想发送您所在位置的坐标,我会尝试将您的代码更改为:
- (IBAction)send:(id)sender {
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
[controller setMessageComposeDelegate:self];
NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
if([MFMessageComposeViewController canSendText])
{
[controller setRecipients:[NSArray arrayWithObjects:nil]];
[controller setBody:theLocation];
[self presentViewController:controller animated:YES completion:NULL];
}else{
NSLog(@"NO Texting");
}
现在,如果你想在地图上发送带图钉的图片,可以采取几种方法。以编程方式截取并通过电子邮件发送。这都是理论上的代码而不是100%完美,(我不认为......可能是这样):
- (UIImage *) screenshot {
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
[self send:image];
- (IBAction)send:(UIImage *)image {
UIImage *locationImage = image;
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
[controller setMessageComposeDelegate:self];
if([MFMessageComposeViewController canSendText]) {
[controller setRecipients:[NSArray arrayWithObjects:nil]];
[controller setBody:locationImage];
[self presentViewController:controller animated:YES completion:NULL];
}
else {
NSLog(@"NO Texting");
}
}