EaseLocationViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import <CoreLocation/CoreLocation.h>
  13. #import <MapKit/MapKit.h>
  14. #import "EaseLocationViewController.h"
  15. #import "UIViewController+HUD.h"
  16. #import "EaseLocalDefine.h"
  17. static EaseLocationViewController *defaultLocation = nil;
  18. @interface EaseLocationViewController () <MKMapViewDelegate,CLLocationManagerDelegate>
  19. {
  20. MKMapView *_mapView;
  21. MKPointAnnotation *_annotation;
  22. CLLocationManager *_locationManager;
  23. CLLocationCoordinate2D _currentLocationCoordinate;
  24. BOOL _isSendLocation;
  25. }
  26. @property (strong, nonatomic) NSString *addressString;
  27. @end
  28. @implementation EaseLocationViewController
  29. @synthesize addressString = _addressString;
  30. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  31. {
  32. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  33. if (self) {
  34. _isSendLocation = YES;
  35. }
  36. return self;
  37. }
  38. - (instancetype)initWithLocation:(CLLocationCoordinate2D)locationCoordinate
  39. {
  40. self = [super initWithNibName:nil bundle:nil];
  41. if (self) {
  42. _isSendLocation = NO;
  43. _currentLocationCoordinate = locationCoordinate;
  44. }
  45. return self;
  46. }
  47. + (instancetype)defaultLocation
  48. {
  49. static dispatch_once_t onceToken;
  50. dispatch_once(&onceToken, ^{
  51. defaultLocation = [[EaseLocationViewController alloc] initWithNibName:nil bundle:nil];
  52. });
  53. return defaultLocation;
  54. }
  55. - (void)viewDidLoad
  56. {
  57. [super viewDidLoad];
  58. self.title = NSEaseLocalizedString(@"location.messageType", @"location message");
  59. UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
  60. [backButton setImage:[UIImage imageNamed:@"EaseUIResource.bundle/back"] forState:UIControlStateNormal];
  61. [backButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
  62. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
  63. [self.navigationItem setLeftBarButtonItem:backItem];
  64. _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
  65. _mapView.delegate = self;
  66. _mapView.mapType = MKMapTypeStandard;
  67. _mapView.zoomEnabled = YES;
  68. [self.view addSubview:_mapView];
  69. if (_isSendLocation) {
  70. _mapView.showsUserLocation = YES;//显示当前位置
  71. UIButton *sendButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
  72. [sendButton setTitle:NSEaseLocalizedString(@"send", @"Send") forState:UIControlStateNormal];
  73. sendButton.accessibilityIdentifier = @"send_location";
  74. [sendButton setTitleColor:[UIColor colorWithRed:32 / 255.0 green:134 / 255.0 blue:158 / 255.0 alpha:1.0] forState:UIControlStateNormal];
  75. [sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
  76. [sendButton addTarget:self action:@selector(sendLocation) forControlEvents:UIControlEventTouchUpInside];
  77. [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:sendButton]];
  78. self.navigationItem.rightBarButtonItem.enabled = NO;
  79. [self startLocation];
  80. }
  81. else{
  82. [self removeToLocation:_currentLocationCoordinate];
  83. }
  84. }
  85. - (void)didReceiveMemoryWarning
  86. {
  87. [super didReceiveMemoryWarning];
  88. // Dispose of any resources that can be recreated.
  89. }
  90. -(void)viewWillAppear:(BOOL)animated
  91. {
  92. [super viewWillAppear:animated];
  93. }
  94. -(void)viewWillDisappear:(BOOL)animated
  95. {
  96. [super viewWillDisappear:animated];
  97. }
  98. #pragma mark - MKMapViewDelegate
  99. - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
  100. {
  101. __weak typeof(self) weakSelf = self;
  102. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  103. [geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *array, NSError *error) {
  104. if (!error && array.count > 0) {
  105. CLPlacemark *placemark = [array objectAtIndex:0];
  106. weakSelf.addressString = placemark.name;
  107. [self removeToLocation:userLocation.coordinate];
  108. }
  109. }];
  110. }
  111. - (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
  112. {
  113. [self hideHud];
  114. if (error.code == 0) {
  115. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
  116. message:[error.userInfo objectForKey:NSLocalizedRecoverySuggestionErrorKey]
  117. delegate:nil
  118. cancelButtonTitle:NSEaseLocalizedString(@"ok", @"OK")
  119. otherButtonTitles:nil, nil];
  120. [alertView show];
  121. }
  122. }
  123. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
  124. {
  125. switch (status) {
  126. case kCLAuthorizationStatusNotDetermined:
  127. if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
  128. {
  129. [_locationManager requestWhenInUseAuthorization];
  130. }
  131. break;
  132. case kCLAuthorizationStatusDenied:
  133. {
  134. }
  135. default:
  136. break;
  137. }
  138. }
  139. #pragma mark - public
  140. /*!
  141. @method
  142. @brief 开启定位
  143. @discussion
  144. @result
  145. */
  146. - (void)startLocation
  147. {
  148. if([CLLocationManager locationServicesEnabled]){
  149. _locationManager = [[CLLocationManager alloc] init];
  150. _locationManager.delegate = self;
  151. _locationManager.distanceFilter = 5;
  152. _locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//kCLLocationAccuracyBest;
  153. if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
  154. [_locationManager requestWhenInUseAuthorization];
  155. }
  156. }
  157. if (_isSendLocation) {
  158. self.navigationItem.rightBarButtonItem.enabled = NO;
  159. }
  160. [self showHudInView:self.view hint:NSEaseLocalizedString(@"location.ongoning", @"locating...")];
  161. }
  162. /*!
  163. @method
  164. @brief 地图添加大头针
  165. @discussion
  166. @param coords 位置信息
  167. @result
  168. */
  169. -(void)createAnnotationWithCoords:(CLLocationCoordinate2D)coords
  170. {
  171. if (_annotation == nil) {
  172. _annotation = [[MKPointAnnotation alloc] init];
  173. }
  174. else{
  175. [_mapView removeAnnotation:_annotation];
  176. }
  177. _annotation.coordinate = coords;
  178. [_mapView addAnnotation:_annotation];
  179. }
  180. /*!
  181. @method
  182. @brief 大头针移动到指定位置
  183. @discussion
  184. @param locationCoordinate 指定位置
  185. @result
  186. */
  187. - (void)removeToLocation:(CLLocationCoordinate2D)locationCoordinate
  188. {
  189. [self hideHud];
  190. _currentLocationCoordinate = locationCoordinate;
  191. float zoomLevel = 0.01;
  192. MKCoordinateRegion region = MKCoordinateRegionMake(_currentLocationCoordinate, MKCoordinateSpanMake(zoomLevel, zoomLevel));
  193. [_mapView setRegion:[_mapView regionThatFits:region] animated:YES];
  194. if (_isSendLocation) {
  195. self.navigationItem.rightBarButtonItem.enabled = YES;
  196. }
  197. [self createAnnotationWithCoords:_currentLocationCoordinate];
  198. }
  199. - (void)sendLocation
  200. {
  201. if (_delegate && [_delegate respondsToSelector:@selector(sendLocationLatitude:longitude:andAddress:)]) {
  202. [_delegate sendLocationLatitude:_currentLocationCoordinate.latitude longitude:_currentLocationCoordinate.longitude andAddress:_addressString];
  203. }
  204. [self.navigationController popViewControllerAnimated:YES];
  205. }
  206. @end