ios 8地图定位服务问题

来源:互联网 发布:js渲染 编辑:程序博客网 时间:2024/05/20 16:45

1、ios 8地图定位出现的问题

项目工程里,需要做一个地图的定位,就是定位附近最近的某个点的位置。并在地图上标示出来。

以前的ios 7版本下的地图定位,在ios 8环境下失去了效果。查看下文档,发现ios 8新增了以下方法;

- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);

- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);

分为以下几步来解决这个问题:

第一步:

location = [[CLLocationManager alloc]init];

location.delegate=self;

[locationrequestAlwaysAuthorization];

第二步:在工程的.plist中添加以下两个字段,如下:(必须有,最少一个,内容是系统ALert的文言,文言可为空)

第三步:在代码中如果直接调用[location requestAlwaysAuthorization];在Xcode 5.0以及5.1版本会出现报错,因为之前没有这个requestAlwaysAuthorization方法,所以在编译时就会报以下错误。说明在ios 8之前CLLocation这个方法根本就没定义这个方法。所以,直接在代码中调用,会导致你在Xcode 5.0的版本编译不过。

 经过一番尝试,采取以下方法来缓解这个问题:

首先采取预编译的方法,来解决编译错误:

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
#define RequestAlwaysAuthorization(a) [a requestAlwaysAuthorization]
#else
#define RequestAlwaysAuthorization(a) ((void)0)
#endif

在代码中,实现如下,来解决运行问题;

 if (wOsversion >= 8.0)
 {
     //设置定位权限,仅ios8有意义
     [RequestAlwaysAuthorization(locationManager);
 }

采取以上方法之后,我在xcode 5.1以及xcode 6 beta均可以编译运行通过。

2、参考资料

https://github.com/intuit/LocationManager

http://www.cocoachina.com/bbs/simple/?t217107.html

http://www.cocoachina.com/ask/questions/show/116838

https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW26

0 0
原创粉丝点击