使用AKLocationManager定位

使用AKLocationManager定位

使用AKLocationManager定位

https://github.com/ideaismobile/AKLocationManager

以下是使用情况:

使用AKLocationManager定位

是不是很简单呢,我们可以将它的步骤进一步封装,让它更简单!

源码如下:

YXLocationManager.h + YXLocationManager.m

//
//  YXLocationManager.h
//  MoreMapInfo
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

typedef void(^locationBlock_t)(CLLocation* location, NSError *error);

@interface YXLocationManager : NSObject

+ (void)getCurrentLocation:(locationBlock_t)locationBlock;

@end
//
//  YXLocationManager.m
//  MoreMapInfo
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "YXLocationManager.h"
#import "AKLocationManager.h"

@implementation YXLocationManager

+ (void)getCurrentLocation:(locationBlock_t)locationBlock
{
    // 设置定位精度(这个是必须设置的,否则无法定位)
    [AKLocationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    
    // 设置超时时间
    [AKLocationManager setTimeoutTimeInterval:10];
    
    // 先结束获取坐标
    [AKLocationManager stopLocating];
    
    // 开始定位坐标
    [AKLocationManager startLocatingWithUpdateBlock:^(CLLocation* location)
     {
         locationBlock(location, nil);
         
     }failedBlock:^(NSError *error)
     {
         locationBlock(nil, error);
     }];
}


@end

使用AKLocationManager定位

是不是很简单呢:).

附录:

// 动画定位到自身位置

            [self.mapView setCenterCoordinate:location.coordinate
                                     animated:YES];