发送用户当前位置 Xcode

Sending Users Current Location Xcode

我希望我的应用程序能够像 Apple 地图在截屏中那样向用户发送位置信息。我读过这个答案“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

那么 "send" 对用户来说,它是什么意思?如果您只想发送您所在位置的坐标,我会尝试将您的代码更改为:

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