在iOS8的设备上,使用高德地图SDK你会发现MAMapView里的回调位置是空的。
-(void)mapView:(MAMapView*)mapView didUpdateUserLocation:(MAUserLocation*)userLocation updatingLocation:(BOOL)updatingLocation { CLLocation *currentLocation = userLocation.location; if (currentLocation) { } }
在iOS8上currentLocation是空的,导致定位失败了。我们知道苹果在iOS8上对定位进行了大幅度优化,可以支持室内定位,常去地点统计,楼层等。
高德失败的原因可能是未对iOS8做适配。
解决方法是:
1.工程的info.plist添加NSLocationWhenInUseDescription,NSLocationAlwaysUsageDescription字段,不同的字段对应的方法不同
2.在AppDelegate.m中声明个CLLocationManager私有变量,代码如下:
@interface AppDelegate()<CLLocationManagerDelegate> { UINavigationController *_navController; CLLocationManager *_locationmanager; } @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [UIApplication sharedApplication].idleTimerDisabled = TRUE; _locationmanager = [[CLLocationManager alloc] init]; [_locationmanager requestAlwaysAuthorization]; //NSLocationAlwaysUsageDescription [_locationmanager requestWhenInUseAuthorization]; //NSLocationWhenInUseDescription _locationmanager.delegate = self; }
-(void)mapView:(MAMapView*)mapView didUpdateUserLocation:(MAUserLocation*)userLocation updatingLocation:(BOOL)updatingLocation就可以正常获取用户当前位置了,此时userLocation.location是有值的。