单次定位不带地图展示—带逆地理定位

在iOS中, 地图开发经常会用到的三个框架, 一个是苹果系统自带的MapKit框架, 另外两个, 一个是百度地图BMKMapView, 另一个是高德地图MAMapView.

使用原生地图

使用原生地图 和定位
MapKit:地图框架显示地图
CoreLocation:定位框架, 没有地图时也可以使用定位.

#import 
@interface SignShakeViewController ()
{
    CLLocationManager *_locationManager;
    NSString *lat;
    NSString *lon;
    NSString *name;
}

初始化定位管理器

#pragma mark 初始化定位管理器
- (void)initializeLocationService {
    
    BOOL isEnable = [CLLocationManager locationServicesEnabled];
    CGFloat version = [[UIDevice currentDevice].systemVersion doubleValue];//float
    
    if(isEnable) {
        if(!_locationManager){
            
            // 初始化定位管理器
            _locationManager = [[CLLocationManager alloc] init];
            // 设置代理
            _locationManager.delegate = self;
            // 设置定位精确度到米
            _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            // 设置过滤器为无
            _locationManager.distanceFilter = kCLDistanceFilterNone;
            
            if(version > 8.0f){
                // 请求定位服务
                // 取得定位权限,有两个方法,取决于你的定位使用情况
                // 一个是requestAlwaysAuthorization,一个是requestWhenInUseAuthorization
                
                // iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法
                // 在Info.plist文件中添加如下配置:
                // NSLocationAlwaysUsageDescription Always
                // NSLocationWhenInUseUsageDescription InUse
                [_locationManager requestAlwaysAuthorization];//这句话ios8以上版本使用。
            }
            
            [_locationManager startUpdatingLocation];
        }
    }
    else{
        NSLog(@"当前设备定位功能未开启!,建议您到手机系统的[设置]->[隐私]->[定位服务]中打开定位服务");
        return;
    }
    
}

实现CLLocationManagerDelegate的代理方法

#pragma mark - 实现CLLocationManagerDelegate的代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // 取出第一个位置
    CLLocation *location = [locations lastObject];
    // 位置坐标
    CLLocationCoordinate2D coordinate = location.coordinate;
    NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);
    
    lat = [NSString stringWithFormat:@"%f",coordinate.latitude];
    lon = [NSString stringWithFormat:@"%f",coordinate.longitude];

    // 反地理编码
    CLGeocoder *_geocoder;
    _geocoder = [[CLGeocoder alloc]init];
    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks firstObject];
        //        NSLog(@"详细信息:%@",placemark.addressDictionary);
        
        // 1.CountryCode CN
        // 2.Country 中国
        // 3.State 云南省
        // 4.City 昆明市
        // 5.SubLocality 五华区
        // 6.Street 学府路183号
        // 7.Thoroughfare 学府路
        // 8.SubThoroughfare 183号
        // 9.Name 中国云南省昆明市五华区莲华街道学府路183号
        NSDictionary *addressDictionary = placemark.addressDictionary;
        name = [addressDictionary objectForKey:@"Name"];
        

    }];
    
    
    // 如果不需要实时定位,使用完关闭定位服务
    [manager stopUpdatingLocation];
}

定位失败,回调此方法

// 定位失败,回调此方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"error :%@",[error localizedDescription]);
    
    if ([error code]==kCLErrorDenied) {
        NSLog(@"访问被拒绝");
    }
    if ([error code]==kCLErrorLocationUnknown) {
        NSLog(@"无法获取位置信息");
    }
}

使用高德地图API

//
//  SingleLocaitonAloneViewController.m
//  FieldStaff
//
//  Created by ChenQianPing on 16/5/2.
//  Copyright © 2016年 chenqianping. All rights reserved.
//

#import "SingleLocaitonAloneViewController.h"
#import "UrlDefine.h"

#import 

#define LocationTimeout 3  //   定位超时时间,可修改,最小2s
#define ReGeocodeTimeout 3 //   逆地理请求超时时间,可修改,最小2s

@interface SingleLocaitonAloneViewController () 

@property (nonatomic, strong) AMapLocationManager *locationManager;
@property (nonatomic, copy) AMapLocatingCompletionBlock completionBlock;

@end

@implementation SingleLocaitonAloneViewController

- (void)configLocationManager
{
    
    [AMapLocationServices sharedServices].apiKey = APIKey;
    
    self.locationManager = [[AMapLocationManager alloc] init];
    
    [self.locationManager setDelegate:self];
    
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    
    [self.locationManager setPausesLocationUpdatesAutomatically:NO];
    
    [self.locationManager setAllowsBackgroundLocationUpdates:YES];
    
    [self.locationManager setLocationTimeout:LocationTimeout];
    
    [self.locationManager setReGeocodeTimeout:ReGeocodeTimeout];
}

- (void)reGeocodeAction
{
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
}

- (void)cleanUpAction
{
    [self.locationManager stopUpdatingLocation];
    
    [self.locationManager setDelegate:nil];
}

- (void)initCompleteBlock
{
    self.completionBlock = ^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error)
    {
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            
            if (error.code == AMapLocationErrorLocateFailed)
            {
                return;
            }
        }
        
        if (location)
        {
            if (regeocode)
            {
                // regeocode.citycode           0871
                // regeocode.adcode             530102
                // regeocode.city               昆明市
                // location.horizontalAccuracy  100.00m
                
                NSString *title = regeocode.city;
                if (title.length == 0)
                {
                    title = regeocode.province;
                }
                
                NSString *displayLabel = [NSString stringWithFormat:@"%@ \n %@-%@-%@", regeocode.formattedAddress,regeocode.citycode, regeocode.adcode, title];
                
                NSLog(@"displayLabel---%@",displayLabel);
                NSLog(@"location.coordinate.latitude---%f",location.coordinate.latitude);
                NSLog(@"location.coordinate.longitude---%f",location.coordinate.longitude);
                

            }
            else
            {
                NSString *displayLabel = [NSString stringWithFormat:@"lat:%f;lon:%f \n accuracy:%.2fm", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy];
                
                NSLog(@"displayLabel---%@",displayLabel);

            }
        }
    };
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initCompleteBlock];
    
    [self configLocationManager];
    
    [self reGeocodeAction];
}

@end

你可能感兴趣的:(单次定位不带地图展示—带逆地理定位)