iOS8 高德地图SDK MAMapView无法定位的问题

来源:互联网 发布:学java以后工作干什么 编辑:程序博客网 时间:2024/05/01 20:17

在iOS8的设备上,使用高德地图SDK你会发现MAMapView里的回调位置是空的。

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(void)mapView:(MAMapView*)mapView didUpdateUserLocation:(MAUserLocation*)userLocation updatingLocation:(BOOL)updatingLocation  
  2. {  
  3.     CLLocation *currentLocation = userLocation.location;  
  4.     if (currentLocation) {  
  5.     }  
  6. }  

在iOS8上currentLocation是空的,导致定位失败了。我们知道苹果在iOS8上对定位进行了大幅度优化,可以支持室内定位,常去地点统计,楼层等。

高德失败的原因可能是未对iOS8做适配。

解决方法是:

1.工程的info.plist添加NSLocationWhenInUseDescription,NSLocationAlwaysUsageDescription字段,不同的字段对应的方法不同

2.在AppDelegate.m中声明个CLLocationManager私有变量,代码如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @interface AppDelegate()<CLLocationManagerDelegate>  
  2. {  
  3.     UINavigationController *_navController;  点击打开链接
  4.     CLLocationManager      *_locationmanager;  
  5. }  
  6.   
  7. @end  
  8.   
  9. @implementation AppDelegate  
  10. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  11. {  
  12.     [UIApplication sharedApplication].idleTimerDisabled = TRUE;  
  13.       
  14.     _locationmanager = [[CLLocationManager alloc] init];  
  15.     [_locationmanager requestAlwaysAuthorization];        //NSLocationAlwaysUsageDescription  
  16.     [_locationmanager requestWhenInUseAuthorization];     //NSLocationWhenInUseDescription  
  17.     _locationmanager.delegate = self;  
  18. }  

这样在MAMapView的回调
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(void)mapView:(MAMapView*)mapView didUpdateUserLocation:(MAUserLocation*)userLocation updatingLocation:(BOOL)updatingLocation  

就可以正常获取用户当前位置了,此时userLocation.location是有值的。

转载地址:http://blog.csdn.net/johnzhjfly/article/details/39497751

高德API定位:http://lbs.amap.com/api/ios-sdk/guide/location/

0 0