您的位置:首页 > 移动开发 > IOS开发

iOS 使用高德地图 MAMapView 无法定位的问题

2015-04-15 11:40 661 查看
//模拟器是无法定位的,必须真机上

http://blog.csdn.net/johnzhjfly/article/details/39497751

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

[objc] view
plaincopy





-(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私有变量,代码如下:

[objc] view
plaincopy





@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;

}

这样在MAMapView的回调

[objc] view
plaincopy





-(void)mapView:(MAMapView*)mapView didUpdateUserLocation:(MAUserLocation*)userLocation updatingLocation:(BOOL)updatingLocation

就可以正常获取用户当前位置了,此时userLocation.location是有值的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: