前期准备--高德地图本地定位-02

直接上代码:

使用的高德的api

//
//  SecondViewController.m
//  GPS闹钟
//
//  Created by aslan on 16/3/25.
//  Copyright © 2016年 aslan. All rights reserved.
//

#import "SecondViewController.h"
#import <MapKit/MapKit.h>
#import <AMapLocationKit/AMapLocationKit.h>


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

@interface SecondViewController ()<AMapLocationManagerDelegate,MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@property (nonatomic, strong) AMapLocationManager *locationManager;

@property (nonatomic, copy) AMapLocatingCompletionBlock completionBlock;

@property (weak, nonatomic) IBOutlet UILabel *displayLabel;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initMapView];
    
 

    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//初始化MapView
- (void)initMapView
{
    _mapView.mapType = MKMapTypeStandard;//标准模式
    _mapView.zoomEnabled = YES;//支持缩放
    _mapView.delegate = self; //设置代理
    
}

/*点击定位按钮*/
- (IBAction)dingweiClick:(id)sender {
    
    self.mapView.showsUserLocation = YES; //显示自己

    //先定义代码块
    [self initCompleteBlock];
    
    //配置manager
    [self configLocationManager];
    
    //进行单次定位
    [self locAction];
    

}

#pragma mark- 配置Manager
- (void)configLocationManager
{
    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)locAction
{
    //单次定位
    [self.locationManager requestLocationWithReGeocode:NO completionBlock:self.completionBlock];
}

- (void)initCompleteBlock
{
    __weak SecondViewController *wSelf = self;
    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)
            {
                [wSelf.displayLabel setText:[NSString stringWithFormat:@"%@ \n %@-%@-%.2fm", regeocode.formattedAddress,regeocode.citycode, regeocode.adcode, location.horizontalAccuracy]];
            }
            else
            {
                [wSelf.displayLabel setText:[NSString stringWithFormat:@"lat:%f;lon:%f \n accuracy:%.2fm", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy]];
                
            }
        }
    };
}

#pragma mark MKMapView的代理

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{

    
    CLLocationCoordinate2D pos = {userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude};//生成坐标
    
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,500, 500);//以pos为中心,显示500米
    MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
    [_mapView setRegion:adjustedRegion animated:YES];
}

@end


你可能感兴趣的:(前期准备--高德地图本地定位-02)