EMLocationViewController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // EMLocationViewController.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2019/1/29.
  6. // Copyright © 2019 XieYajie. All rights reserved.
  7. //
  8. #import <CoreLocation/CoreLocation.h>
  9. #import <MapKit/MapKit.h>
  10. #import "EMLocationViewController.h"
  11. @interface EMLocationViewController ()<MKMapViewDelegate, CLLocationManagerDelegate>
  12. @property (nonatomic) BOOL canSend;
  13. @property (nonatomic) CLLocationCoordinate2D locationCoordinate;
  14. @property (nonatomic, strong) NSString *address;
  15. @property (nonatomic, strong) MKMapView *mapView;
  16. @property (nonatomic, strong) MKPointAnnotation *annotation;
  17. @property (nonatomic, strong) CLLocationManager *locationManager;
  18. @end
  19. @implementation EMLocationViewController
  20. - (instancetype)init
  21. {
  22. self = [super init];
  23. if (self) {
  24. _canSend = YES;
  25. }
  26. return self;
  27. }
  28. - (instancetype)initWithLocation:(CLLocationCoordinate2D)aLocationCoordinate
  29. {
  30. self = [super init];
  31. if (self) {
  32. _canSend = NO;
  33. _locationCoordinate = aLocationCoordinate;
  34. }
  35. return self;
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. // Do any additional setup after loading the view.
  40. [self _setupSubviews];
  41. if (self.canSend) {
  42. self.mapView.showsUserLocation = YES;//显示当前位置
  43. [self _startLocation];
  44. } else {
  45. [self _moveToLocation:self.locationCoordinate];
  46. }
  47. }
  48. #pragma mark - Subviews
  49. - (void)_setupSubviews
  50. {
  51. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
  52. self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
  53. [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_white"] forBarMetrics:UIBarMetricsDefault];
  54. [self.navigationController.navigationBar.layer setMasksToBounds:YES];
  55. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close_gray"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(closeAction)];
  56. if (self.canSend) {
  57. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(sendAction)];
  58. }
  59. self.title = @"地理位置";
  60. self.view.backgroundColor = [UIColor whiteColor];
  61. self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
  62. self.mapView.delegate = self;
  63. self.mapView.mapType = MKMapTypeStandard;
  64. self.mapView.zoomEnabled = YES;
  65. [self.view addSubview:self.mapView];
  66. self.annotation = [[MKPointAnnotation alloc] init];
  67. }
  68. #pragma mark - Private
  69. - (void)_startLocation
  70. {
  71. if ([CLLocationManager locationServicesEnabled]) {
  72. _locationManager = [[CLLocationManager alloc] init];
  73. _locationManager.delegate = self;
  74. _locationManager.distanceFilter = 5;
  75. _locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//kCLLocationAccuracyBest;
  76. if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
  77. [_locationManager requestWhenInUseAuthorization];
  78. }
  79. }
  80. // [self showHudInView:self.view hint:@"正在定位..."];
  81. }
  82. - (void)_moveToLocation:(CLLocationCoordinate2D)locationCoordinate
  83. {
  84. // [self hideHud];
  85. self.locationCoordinate = locationCoordinate;
  86. float zoomLevel = 0.01;
  87. MKCoordinateRegion region = MKCoordinateRegionMake(self.locationCoordinate, MKCoordinateSpanMake(zoomLevel, zoomLevel));
  88. [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
  89. [self.mapView removeAnnotation:self.annotation];
  90. self.annotation.coordinate = locationCoordinate;
  91. [self.mapView addAnnotation:self.annotation];
  92. }
  93. #pragma mark - MKMapViewDelegate
  94. - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
  95. {
  96. __weak typeof(self) weakself = self;
  97. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  98. [geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *array, NSError *error) {
  99. if (!error && array.count > 0) {
  100. CLPlacemark *placemark = [array objectAtIndex:0];
  101. weakself.address = placemark.name;
  102. [weakself _moveToLocation:userLocation.coordinate];
  103. }
  104. }];
  105. }
  106. - (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
  107. {
  108. // [self hideHud];
  109. if (error.code == 0) {
  110. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:[error.userInfo objectForKey:NSLocalizedRecoverySuggestionErrorKey] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  111. [alertView show];
  112. }
  113. }
  114. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
  115. {
  116. switch (status) {
  117. case kCLAuthorizationStatusNotDetermined:
  118. if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
  119. [self.locationManager requestWhenInUseAuthorization];
  120. }
  121. break;
  122. case kCLAuthorizationStatusDenied:
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. #pragma mark - Action
  129. - (void)closeAction
  130. {
  131. [self dismissViewControllerAnimated:YES completion:nil];
  132. }
  133. - (void)sendAction
  134. {
  135. [self dismissViewControllerAnimated:YES completion:^{
  136. if (self.sendCompletion) {
  137. self.sendCompletion(self.locationCoordinate, self.address);
  138. }
  139. }];
  140. }
  141. @end