定位

来源:互联网 发布:c语言怎么取反 编辑:程序博客网 时间:2024/05/14 05:07

实现功能:定位所在地的地址,及地址所在的区域地图

利用技术:iOS原生技术,两大框架:CoreLocation、MapKit    主要用到其中三个对象:CLLocationManager、CLGeocoder、MKMapView

实现图示:


操作提示:运行程序后,点击定位按钮,等待活动指示器消失,若定位成功,则出现所在地的详细地址,点击此地址栏,即进入所在的区域地图,若定位失败,地址栏显示定位失败,且地址栏不可点击进入地图,此时请尝试查看是否授权定位,或是否联网,然后重新运行程序后,再次定位。

运行环境:xcode7.2.1,iOS9.2

核心代码:

⚠️注意:窗口的视图部分由storyboard创建(省略……)。

以下主要为ViewController.h文件的代码实现:

#import "ViewController.h"

#import "INTULocationManager.h"

#import <CoreLocation/CoreLocation.h>

#import <MapKit/MapKit.h>


@interface ViewController ()<CLLocationManagerDelegate>


/** 首页的签到地址 */

@property (weak, nonatomic) IBOutletUILabel *addressLab;


/** 位置管理者 */

@property (nonatomic,strong) CLLocationManager * manager;


/** 编码管理对象 */

@property (nonatomic,strong) CLGeocoder * geocoder;


/** 地图视图 */

@property (nonatomic,strong) MKMapView * mapView;

/** 地图中显示的签到地址 */

@property (nonatomic,strong) UILabel *address;


/** 经纬度 */

@property (nonatomic,strong) CLLocation *location;


/** 活动指示器 */

@property (nonatomic,strong) UIActivityIndicatorView *indicator;


@end


@implementation ViewController


- (CLLocationManager *)manager

{

    if (!_manager) {

        _manager = [[CLLocationManageralloc] init];

        _manager.delegate =self;

        /** 请求用户授权:

            注意判断条件:1.能够执行次方法,否则报错

                        2.iOS系统8.0以上

        */

        if ([selfrespondsToSelector:@selector(requestAlwaysAuthorization)]||[[UIDevicecurrentDevice].systemVersionfloatValue] >= 8.0) {

            // 请求授权

            [_managerrequestAlwaysAuthorization];

        }

    }

    return_manager;

}


- (CLGeocoder *)geocoder

{

    if (!_geocoder) {

        _geocoder = [[CLGeocoderalloc] init];

    }

    return_geocoder;

}


- (MKMapView *)mapView

{

    if (!_mapView) {

        _mapView = [[MKMapViewalloc] init];

        _mapView.frame =self.view.bounds;

        

        // 地图属性配置

        self.mapView.mapType =MKMapTypeStandard;

        self.mapView.showsUserLocation =YES;


        // 地图中展示的签到地址栏

        UILabel *address = [[UILabelalloc] init];

        address.frame = CGRectMake(20, 20, 300, 30);

        address.font = [UIFontsystemFontOfSize:13];

        address.textColor = [UIColorgrayColor];

        [_mapView addSubview:address];

        self.address = address;

        // 经纬度

    }

    return_mapView;

}


- (UIActivityIndicatorView *)indicator

{

    if (!_indicator) {

        _indicator = [[UIActivityIndicatorViewalloc]initWithFrame:CGRectMake(self.addressLab.center.x,self.addressLab.frame.origin.y, 30, 30)];

        _indicator.activityIndicatorViewStyle =UIActivityIndicatorViewStyleGray;

        [self.viewaddSubview:_indicator];

    }

    return_indicator;

}


- (void)viewDidLoad {

    [superviewDidLoad];

    

    // 隐藏签到地址

    self.addressLab.hidden =YES;

}


/**

 *定位按钮

 */

- (IBAction)locationButtonClicked:(id)sender {


    [self manager];

    

    // 开始更新位置

    [self.managerstartUpdatingLocation];

    

    // 指示器开始

    [self.indicatorstartAnimating];

    NSLog(@"点击定位按钮");


}


#pragma mark -- CLLocationManagerDelegate


/**

 *location--位置更新完成执行的方法

 */

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations


{

    // 结束定位

    [self .managerstopUpdatingLocation];

    

    // locations:是一个按时间排列的数组,取最后一个数即是最新的信息

    CLLocation *location = [locations lastObject];

    self.location = location;

    

    NSLog(@"纬度:%f,经度:%f",location.coordinate.latitude,location.coordinate.longitude);

 

    

    // 拿到经纬度后,进行位置反编码

    [self.geocoderreverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks,NSError * _Nullable error) {

        if (error == nil) {

            // 遍历数组,找出你要的字段

            [placemarks enumerateObjectsUsingBlock:^(CLPlacemark *_Nonnull obj, NSUInteger idx,BOOL * _Nonnull stop) {

                self.addressLab.text = obj.name;

                

                // 指示器停止

                [self.indicatorstopAnimating];

                

                // 定位完成,显示签到地址

                self.addressLab.hidden =NO;

                NSLog(@"显示位置");

            }];

        }else

        {

            NSLog(@"error:%@",error);

            // 定位失败,提示用户

            self.addressLab.hidden =NO;

            self.addressLab.text =@"定位失败";

            // 指示器停止

            [self.indicatorstopAnimating];

        }

    }];

    

}


/**

 *   展示地图:

 *点击首页地址栏调用的方法--要定位成功后才可以进入地图

 */

- (IBAction)showMap:(id)sender {

    //  定位失败,返回,不再执行下面代码

    if ([self.addressLab.textisEqualToString:@"定位失败"])return;

    

    // 设定展示区域

    // span:区域所跨的纬度、经度

    MKCoordinateSpan span =MKCoordinateSpanMake(0.024983, 0.016953);

    MKCoordinateRegion region =MKCoordinateRegionMake(self.location.coordinate, span);

    [self.mapViewsetRegion:region animated:YES];

    

    self.address.text = [NSStringstringWithFormat:@"定位地址:%@",self.addressLab.text];

  

    [self.viewaddSubview:_mapView];

    

}


@end




0 0
原创粉丝点击