在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(){ 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是有值的。