#import "CDViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface CDViewController ()
@end
@implementation CDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = [[CLLocationManager alloc] init];
self.currentHeading = [[CLHeading alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.headingFilter = 1;
self.locationManager.delegate = self;
[self.locationManager startUpdatingHeading];
NSLog(@"%f",self.locationManager.location.coordinate.latitude);
NSLog(@"%f",self.locationManager.location.coordinate.longitude);
_startLocation = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
self.currentHeading = newHeading;
self.headingLabel.text = [NSString stringWithFormat:@"%d°", (int)newHeading.magneticHeading];
}
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
{
if(self.currentHeading == nil)
{
return YES;
}
else
{
return NO;
}
}
#pragma mark -
#pragma mark CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manage didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSString *currentLatitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.coordinate.latitude];
_latitude.text = currentLatitude;
NSString *currentLongitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.coordinate.longitude];
_longitude.text = currentLongitude;
NSString *currentHorizontalAccuracy =
[[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.horizontalAccuracy];
_horizontalAccuracy.text = currentHorizontalAccuracy;
NSString *currentAltitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.altitude];
_altitude.text = currentAltitude;
NSString *currentVerticalAccuracy =
[[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.verticalAccuracy];
_verticalAccuracy.text = currentVerticalAccuracy;
if (_startLocation == nil)
_startLocation = newLocation;
CLLocationDistance distanceBetween = [newLocation
distanceFromLocation:_startLocation];
NSString *tripString = [[NSString alloc]
initWithFormat:@"%f",
distanceBetween];
_distance.text = tripString;
}
-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
}
@end
.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface CDViewController : UIViewController<UIApplicationDelegate,CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *headingLabel;
@property (nonatomic,retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLHeading * currentHeading;
@property (strong, nonatomic) IBOutlet UILabel *latitude;
@property (strong, nonatomic) IBOutlet UILabel *longitude;
@property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracy;
@property (strong, nonatomic) IBOutlet UILabel *altitude;
@property (strong, nonatomic) IBOutlet UILabel *verticalAccuracy;
@property (strong, nonatomic) IBOutlet UILabel *distance;
@property (strong, nonatomic) CLLocation *startLocation;
@end
Add one more line -
[self.locationManager startUpdatingLocation];
for reference
http://stackoverflow.com/questions/22958946/cllocationmanager-delegate-method-didupdatetolocation-not-getting-called
Sign up here with your email
5 comments
Write commentsNSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription keys to the app's Info.plist.
Reply- (void)viewDidLoad {
Reply[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager requestAlwaysAuthorization];
[_locationManager startUpdatingLocation];
NSLog(@"%f",self.locationManager.location.coordinate.latitude);
NSLog(@"%f",self.locationManager.location.coordinate.longitude);
_startLocation = nil;
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSString *longitude=[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
NSString *latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSLog(@"%@",latitude);
NSLog(@"%@",longitude);
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D myCoordinate;
myCoordinate.latitude=latitude.floatValue;
myCoordinate.longitude=longitude.floatValue;
annotation.coordinate = myCoordinate;
[self.mapView removeAnnotations:[_mapView annotations]];
[self.mapView addAnnotation:annotation];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = latitude.floatValue ;
region.center.longitude = longitude.floatValue;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[self.mapView setRegion:region animated:YES];
[_mapView setDelegate:self];
}
}
#pragma mark MKMapView delegate
- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id )annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
return annotationView;
else
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"AnnotationIdentifier"] ;
annotationView.canShowCallout = YES;
annotationView.image =[UIImage imageNamed:@"person.png"];
annotationView.draggable = YES;
return annotationView;
}
return nil;
}
I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
ReplyAndroid App Development Company
mobile App Development Company
Android App Development Company
Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
Replyhttps://textmunication.com/
https://textmunication.com/solutions/text-message-marketing/
https://textmunication.com/industries/fitness-sms-mobile-marketing/
ConversionConversion EmoticonEmoticon