0

I am a new in ios programming. I'm having problems trying to show a map showing a pin with the current user position.

@implementation ViewController 
@synthesize mapView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.locationManager = [[CLLocationManager alloc]init];
    self.locationManager.delegate = self;
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

    if (authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
        authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
        authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {

        [self.locationManager startUpdatingLocation];
        mapView.showsUserLocation = YES;
        [self.mapView delegate];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

info.plist has NSLocationWhenIsUseUsageDescription property, but it is not displaying the pin nor making zoom to the user location.

HyLian
  • 4,999
  • 5
  • 33
  • 40
  • http://stackoverflow.com/questions/4152003/how-can-i-get-current-location-from-user-in-ios OR http://stackoverflow.com/questions/3974703/how-to-show-current-location-on-mkmapview – Rohit Khandelwal Dec 23 '15 at 13:16
  • See this http://stackoverflow.com/questions/24717547/ios-8-map-kit-obj-c-cannot-get-users-location – Wolverine Dec 23 '15 at 13:21

2 Answers2

0

Try this code instead, you are asking it to start updating location inside the if block which doesn't seem right to me.

And also I am not sure what you are trying to do with this line inside the if block.

Apart from this there can be multiple issues in your approach.. I would advise you to look at a good tutorial for this.

[self.mapView delegate];

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

if (authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {

    mapView.showsUserLocation = YES;
    [self.mapView delegate];

}
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
0

You might want to look into the didUpdateUserLocation method. After you ask for a map to display the User Location, it can take some time for a response. The didUpdateUserLocation method is accessed when the system gets back to you with data about the User Location. didUpdateUserLocation is the place where you'd put your code to do whatever you want to do with the map now that you have the user location.

GlennRay
  • 959
  • 9
  • 18