Current position doesn't show up in MapView (iOS)

884 views Asked by At

I have set

self.mapView.showsUserLocation = YES;

However, I still cannot see the current pin in Map.

What am I missing?

Here's viewDidLoad

- (void)viewDidLoad {
self.mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
    [self.locationManager requestAlwaysAuthorization];
}
#endif

[self.locationManager startUpdatingLocation];

self.mapView.showsUserLocation = YES;
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];

MKCoordinateRegion region;
region.center.latitude = 25.03;  
region.center.longitude = 121.5;
region.span.latitudeDelta = 0.2;
region.span.longitudeDelta = 0.2;

[self.mapView setRegion:region animated:YES];
}

And mapView

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation*)userLocation
{
double X= userLocation.coordinate.latitude;
double Y= userLocation.coordinate.longitude;

NSLog(@"%f,%f",X,Y);

CLLocationCoordinate2D currentPosition = [userLocation coordinate];
MKCoordinateRegion region =MKCoordinateRegionMakeWithDistance(currentPosition, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}

Thanks!!

1

There are 1 answers

0
SanitLee On

There's an issue with location authorisation on iOS8. To solve that, please make sure you add one or both of the following keys to your Info.plist file:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

And replace this part in your code:

#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
    [self.locationManager requestAlwaysAuthorization];
}
#endif 

With this:

// Add this to solve a location authorization issue on iOS8
// See more at: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
if ([self._locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self._locationManager requestWhenInUseAuthorization];
}

Hope this helps.