美文网首页
iOS自带定位的使用

iOS自带定位的使用

作者: 红先生的小简书 | 来源:发表于2017-09-18 11:53 被阅读0次

配置:

  1. info.plist中配置Privacy - Location When In Use Usage Description
  2. 导入库文件#import <CoreLocation/CoreLocation.h>

代码:

#import "RootViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface RootViewController ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

#implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self startLocation];
}

// 开启定位
- (void)startLocation {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;//最精准
    
    /** 由于IOS8中定位的授权机制改变 需要进行手动授权
     * 获取授权认证,两个方法:
     * [self.locationManager requestWhenInUseAuthorization];
     * [self.locationManager requestAlwaysAuthorization];
     */
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        NSLog(@"requestWhenInUseAuthorization");
        //  [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager requestAlwaysAuthorization];
    }
    // 开始定位,不断调用其代理方法
    [self.locationManager startUpdatingLocation];
}

#pragma mark  【 CLLocationManagerDelegate 】
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    // 1.获取用户位置的对象
    CLLocation *location = [locations lastObject];
    CLLocationCoordinate2D coordinate = location.coordinate;
    NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude);
    longitute = coordinate.longitude;
    latitude = coordinate.latitude;
    
    // 获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
     {
         for (CLPlacemark * placemark in placemarks) {
             
             NSString *city = placemark.locality;
             if (!city) {
                 //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                 city = placemark.administrativeArea;
             }
             NSDictionary *dictionary = [placemark addressDictionary];
             NSLog(@"国家:%@",[dictionary objectForKey:@"Country"]);
             NSLog(@"省:%@",[dictionary objectForKey:@"State"]);
             NSLog(@"市:%@",city);
             NSLog(@"区:%@",placemark.subLocality);
             NSLog(@"街道:%@",placemark.thoroughfare);
             NSLog(@"子街道:%@",placemark.subThoroughfare);
         }
         if (error == nil && [placemarks count] == 0) {
             NSLog(@"No results were returned.");
         } else if (error != nil) {
             NSLog(@"An error occurred = %@", error);
         }
     }];
    
    // 2.停止定位
    [manager stopUpdatingLocation];
}

@end

相关文章

  • iOS自带定位的使用

    配置: info.plist中配置Privacy - Location When In Use Usage Des...

  • 百度地图之定位

    iOS中三种定位方式 ioS开发之CoreLocation(GPS定位) iOS自带的GPS 定位 iOS开发 C...

  • iOS使用苹果自带定位功能

    由于公司的项目中只需要一个定位的功能,于是想到了苹果自带定位功能。废话不多说,直接上代码。 实现、 1、导入头文件...

  • CLLocationManager定位和ibeacon的检测

    一.iOS自带的定位 iOS通过自带的CoreLocation 框架可以获取用户的当前位置,城市,经纬度等信息。 ...

  • ios定位到具体道路

    ios自带定位系统,使用系统定位到具体的经纬度,然后根据反地理编码编译出对应的城市区以及道路等,废话不多说,直接上...

  • iOS 系统自带定位服务

    iOS 自带定位服务(原创) ps:本文粘贴自别处,只为学习记录~ 定位服务 iOS 7 提供了4种不同的途径进行...

  • IOS后台定位以及位置上传方案

    IOS后台定位以及位置上传方案 iOS定位原理和使用建议 iOS后台持续定位并定时上传 iOS 通过定位获取常驻后...

  • 百度地图初体验

    基本功能 地图百度地图的基本使用和苹果自带的Mapkit差不多,很多方法都是类似的 定位iOS9之后定位权限只有系...

  • iOS定位工具类--block传值

    在iOS开发中,经常会碰到定位到当前城市的需求,系统自带的定位功能就能实现,于是封装了一个类方便使用,整理一下发出...

  • iOS开发之集成高德地图(一)

    在iOS开发中,经常会使用到定位和地图,Apple自带的定位和地图应付一般的场景绰绰有余,但是也缺少一些功能,如P...

网友评论

      本文标题:iOS自带定位的使用

      本文链接:https://www.haomeiwen.com/subject/pnnvsxtx.html