使用FCCurrentLocationGeocoder获取用户位置信息

FCCurrentLocationGeocoder封装了原生的iOS定位api,可以使用block形式获取用户位置信息,但是使用过程中也有不少坑。
1、git地址
2、如果你使用的CocoaPods管理第三方库,那么在引用FCCurrentLocationGeocoder的时候,还需要引用FCIPAddressGeocoder,这个是对ip进行反地理编码的第三方库,并且在引用这两个库的时候,不能使用官方的master版本,需要使用下面的版本

pod 'FCIPAddressGeocoder' , '~> 1.0.0'
pod 'FCCurrentLocationGeocoder', '~> 1.1.11'

默认的master版本为1.1.1和1.1.5
3、在项目中导入头文件,然后使用测试代码

FCCurrentLocationGeocoder *geocoder = [FCCurrentLocationGeocoder new];
geocoder.canPromptForAuthorization = NO; //(optional, default value is YES)
geocoder.canUseIPAddressAsFallback = YES; //(optional, default value is NO. very useful if you need just the approximate user location, such as current country, without asking for permission)
geocoder.timeFilter = 30; //(cache duration, optional, default value is 5 seconds)
geocoder.timeoutErrorDelay = 10; //(optional, default value is 15 seconds)

if ([geocoder canGeocode]) {
    //current-location reverse-geocoding
    [geocoder reverseGeocode:^(BOOL success) {
        
        if(success)
        {
            NSLog(@"%@",geocoder.locationCountry);
            NSLog(@"%@",geocoder.locationCity);
            //you can access the current location using 'geocoder.location'
            //you can access the current location placemarks using 'geocoder.locationPlacemarks'
            //you can access the current location first-placemark using 'geocoder.locationPlacemark'
            //you can access the current location country using 'geocoder.locationCountry'
            //you can access the current location country-code using 'geocoder.locationCountryCode'
            //you can access the current location city using 'geocoder.locationCity'
            //you can access the current location zip-code using 'geocoder.locationZipCode'
            //you can access the current location address using 'geocoder.locationAddress'
        }
        else {
            NSLog(@"%@",geocoder.error);
            //you can debug what's going wrong using: 'geocoder.error'
        }
    }];
}

关键是geocoder.canUseIPAddressAsFallback = YES;属性在FCCurrentLocationGeocoder的版本中没有
4、启动模拟器,需要在debug-->location中选择一个位置,然后运行程序就可以了

遇到问题:
1、在ios8.0之后,还需要在info.plist文件中加入字符串NSLocationWhenInUseUsageDescription,在程序第一次请求数据的时候,会显示你这里输入的字符串
2、ios9.0对于后台定位还有一些特殊的要求,或者想使用系统的定位功能,可以参考下面的文章,写的比较详细
iOS开发之CoreLocation框架(地图/定位)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,266评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,898评论 25 709
  • 时间就是这样,一天天熬着很慢,猛一回头发现很快。今年又接近尾声了,时间过去了,事情还停留在那边。买房的计划只能搁置...
    兰亭小主阅读 157评论 0 0
  • 2017.2.1 作为一名合格的吃货的我,当然不肯放过每一次分享美食的机会了。 吃过那么多美食,我最喜爱的...
    千柚阅读 259评论 2 1
  • baici,那天你跟我说,发现我凡是考试类的东西都很擅长。这几天我仔细考虑了你的话,好像还真是这样。我回忆自己面对...
    蓝贝壳zl阅读 1,991评论 2 0