Jeff Jeff - 22 days ago 11
Objective-C Question

Sending Users Current Location Xcode

Id like my application to send the users location the same way Apple's Maps does in the screenShot. Ive read this answer " link " but I'm not understanding how to implement it. Ive tried this using CLPlaceMark but I'm only able to send the address in a text format. Here is my code. Screen Shot

//.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

Answer

Now what do you mean by "send" it to the user? If you just want to send the coordinates of where you are, I would try changing your code to this:

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

Now if you want to send the picture with the pin on the map, theres a few ways to do it. Programmatically take a screenshot and email it. This is all theoretical code and not 100% perfect, (I don't think... It may be):

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