简体   繁体   中英

iOS 8 Mapview Current Location not fire

MKMapview current user location not fire in iOS-8 ,previous iOS-7 & iOS-6 are working fine.

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

In this line to call automatically the user current location delegate methods

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
}

but it not fire in iOS-8 .

In iOS8, you need to request user's authorization before getting their location.

There are two kinds of request:

-[CLLocationManager requestWhenInUseAuthorization] lets you get users' location only when the app is awaken.

-[CLLocationManager requestAlwaysAuthorization] lets you get users' location even when it's in the background.

You can choose between them accordingly.

For example, put this before you start updating location:

// ask for authorization
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
// check before requesting, otherwise it might crash in older version
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 

     [locationManager requestWhenInUseAuthorization];

}

Furthermore, don't forget to add two keys

NSLocationWhenInUseUsageDescription

and

NSLocationAlwaysUsageDescription

into your info.plist.

Leave the values empty to use the default messages or you can customize your own by inputting the values.

In ios 8 Authorization is required as below

    NSString * osVersion = [[UIDevice currentDevice] systemVersion];
    if ([osVersion floatValue]>= 8.0 ) {
    [_CLLocationManager requestAlwaysAuthorization]; //Requests permission to use location services whenever the app is running. 
// [_CLLocationManager requestWhenInUseAuthorization]; //Requests permission to use location services while the app is in the foreground. 
    }
    [_CLLocationManager startUpdatingLocation];

And you need to add two keys in the plist

1.NSLocationAlwaysUsageDescription

2.NSLocationWhenInUseUsageDescription

在此输入图像描述

In iOS 8 Mapview Current Location fire using

You can set in your .plist file

<key>NSLocationWhenInUseUsageDescription</key>
<string>Your app name Or Other string</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Your app name Or Other string</string>

And your code side you just implement this

CLLocationManager * locationmanager = [CLLocationManager new];

locationmanager.delegate = self;
locationmanager.distanceFilter = kCLDistanceFilterNone;
locationmanager.desiredAccuracy = kCLLocationAccuracyBest;
[locationmanager startUpdatingLocation];    
[locationmanager requestAlwaysAuthorization];

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

location = [locations objectAtIndex:0];
[locationmanager stopUpdatingLocation];

}

I am using this my app get perfect current location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM