2
0

WFCULocationViewController.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // LocationViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/10/28.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCULocationViewController.h"
  9. #import "UIView+Toast.h"
  10. #import "WFCULocationPoint.h"
  11. #import <AddressBookUI/AddressBookUI.h>
  12. #import <CoreLocation/CoreLocation.h>
  13. #import "UIView+Screenshot.h"
  14. @interface WFCULocationViewController () <CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource>
  15. @property(nonatomic, strong) UIBarButtonItem *sendButton;
  16. @property(nonatomic, strong) CLLocationManager *locationManager;
  17. @property(nonatomic, strong) WFCULocationPoint *locationPoint;
  18. @property(nonatomic, strong) CLGeocoder * geoCoder;
  19. @property(nonatomic, strong) CALayer *annotationLayer;
  20. @property(nonatomic, assign) BOOL updateUserLocation;
  21. @property(nonatomic, strong) MKMapView *mapView;
  22. @property(nonatomic, strong) UITableView *locationTableView;
  23. @property(nonatomic, strong) NSMutableArray<CLPlacemark *> *marks;
  24. @property(nonatomic, weak) id<LocationViewControllerDelegate> delegate;
  25. @end
  26. @implementation WFCULocationViewController
  27. - (instancetype)initWithDelegate:(id<LocationViewControllerDelegate>)delegate {
  28. self = [super init];
  29. if (self) {
  30. _locationManager = [[CLLocationManager alloc] init];
  31. _geoCoder = [[CLGeocoder alloc] init];
  32. _delegate = delegate;
  33. }
  34. return self;
  35. }
  36. - (instancetype)initWithLocationPoint:(WFCULocationPoint *)locationPoint{
  37. self = [self initWithNibName:nil bundle:nil];
  38. if (self) {
  39. _locationPoint = locationPoint;
  40. _geoCoder = [[CLGeocoder alloc] init];
  41. }
  42. return self;
  43. }
  44. - (void)viewDidLoad {
  45. [super viewDidLoad];
  46. self.navigationItem.title = @"位置";
  47. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Cancel") style:UIBarButtonItemStylePlain target:self action:@selector(dismiss:)];
  48. if (_locationManager) {
  49. CGRect frame = self.view.bounds;
  50. frame.size.height /= 2;
  51. self.mapView = [[MKMapView alloc] initWithFrame:frame];
  52. frame.origin.y += frame.size.height;
  53. self.locationTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
  54. [self.view addSubview:self.locationTableView];
  55. self.locationTableView.delegate = self;
  56. self.locationTableView.dataSource = self;
  57. self.mapView.showsUserLocation = YES;
  58. self.annotationLayer = [CALayer layer];
  59. UIImage *image = [UIImage imageNamed:@"PinGreen"];
  60. self.annotationLayer.contents = (id)image.CGImage;
  61. self.annotationLayer.frame = CGRectMake(0, 0, 35, 35);
  62. self.annotationLayer.anchorPoint = CGPointMake(0.25f, 0.f);
  63. self.annotationLayer.position = CGPointMake(CGRectGetMidX(self.mapView.bounds), CGRectGetMidY(self.mapView.bounds));
  64. [self setUpRightNavButton];
  65. self.locationPoint = [[WFCULocationPoint alloc] init];
  66. self.locationManager = [CLLocationManager new];
  67. [self.locationManager requestWhenInUseAuthorization];
  68. self.locationManager.delegate = self;
  69. if ([CLLocationManager locationServicesEnabled]) {
  70. [_locationManager requestLocation];
  71. CLAuthorizationStatus status = CLLocationManager.authorizationStatus;
  72. if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied) {
  73. [self.view makeToast:@"请在设置-隐私里允许程序使用地理位置服务"
  74. duration:2
  75. position:CSToastPositionCenter];
  76. }else{
  77. self.mapView.showsUserLocation = YES;
  78. }
  79. }else{
  80. [self.view makeToast:@"请打开地理位置服务"
  81. duration:2
  82. position:CSToastPositionCenter];
  83. }
  84. } else /*if (self.locationPoint)*/ {
  85. self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
  86. MKCoordinateRegion theRegion;
  87. theRegion.center = self.locationPoint.coordinate;
  88. theRegion.span.longitudeDelta = 0.01f;
  89. theRegion.span.latitudeDelta = 0.01f;
  90. [self.mapView addAnnotation:self.locationPoint];
  91. [self.mapView setRegion:theRegion animated:YES];
  92. }
  93. self.mapView.delegate = self;
  94. [self.view addSubview:self.mapView];
  95. }
  96. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  97. }
  98. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations {
  99. if (locations.count) {
  100. [self reverseGeoLocation:locations[0].coordinate];
  101. }
  102. }
  103. - (void)setUpRightNavButton{
  104. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"选定" style:UIBarButtonItemStyleDone target:self action:@selector(onSend:)];
  105. self.navigationItem.rightBarButtonItem = item;
  106. self.sendButton = item;
  107. self.sendButton.enabled = NO;
  108. }
  109. - (void)onSend:(id)sender{
  110. if ([self.delegate respondsToSelector:@selector(onSendLocation:)]) {
  111. UIView * view = [self.mapView viewForAnnotation:self.mapView.userLocation];
  112. view.hidden = YES;
  113. CGRect frame = self.mapView.frame;
  114. UIImage *thumbnail = [self.mapView screenshotWithRect:CGRectMake(0, (frame.size.height - frame.size.width * 2 / 3.f)/2, frame.size.width, frame.size.width * 2 / 3.f)];
  115. self.locationPoint.thumbnail = thumbnail;
  116. [self.delegate onSendLocation:self.locationPoint];
  117. }
  118. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  119. }
  120. - (void)didReceiveMemoryWarning {
  121. [super didReceiveMemoryWarning];
  122. }
  123. #pragma mark - MKMapViewDelegate
  124. - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
  125. if (!_updateUserLocation) {
  126. return;
  127. }
  128. CLLocationCoordinate2D centerCoordinate = mapView.region.center;
  129. [self reverseGeoLocation:centerCoordinate];
  130. [self.annotationLayer removeFromSuperlayer];
  131. }
  132. - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
  133. if (!_updateUserLocation) {
  134. return;
  135. }
  136. [_mapView removeAnnotations:_mapView.annotations];
  137. [self.mapView.layer addSublayer:self.annotationLayer];
  138. }
  139. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
  140. if (annotation == self.mapView.userLocation) {
  141. return nil;
  142. }
  143. if (_locationManager) {
  144. static NSString *reusePin = @"reusePin";
  145. MKPinAnnotationView * pin = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:reusePin];
  146. if (!pin) {
  147. pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reusePin];
  148. }
  149. pin.draggable = YES;
  150. pin.canShowCallout = YES;
  151. return pin;
  152. }
  153. return nil;
  154. }
  155. - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
  156. _updateUserLocation = YES;
  157. MKCoordinateRegion theRegion;
  158. theRegion.center = userLocation.coordinate;
  159. theRegion.span.longitudeDelta = 0.01f;
  160. theRegion.span.latitudeDelta = 0.01f;
  161. [_mapView setRegion:theRegion animated:NO];
  162. }
  163. - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
  164. [_mapView selectAnnotation:self.locationPoint animated:YES];
  165. }
  166. - (void)reverseGeoLocation:(CLLocationCoordinate2D)locationCoordinate2D{
  167. if (self.geoCoder.isGeocoding) {
  168. [self.geoCoder cancelGeocode];
  169. }
  170. CLLocation *location = [[CLLocation alloc]initWithLatitude:locationCoordinate2D.latitude
  171. longitude:locationCoordinate2D.longitude];
  172. __weak typeof(self) ws = self;
  173. self.sendButton.enabled = NO;
  174. [self.geoCoder reverseGeocodeLocation:location
  175. completionHandler:^(NSArray *placemarks, NSError *error) {
  176. if (!error) {
  177. CLPlacemark *mark = [placemarks firstObject];
  178. ws.marks = [placemarks mutableCopy];
  179. NSArray *lines = mark.addressDictionary[@"FormattedAddressLines"];
  180. NSString *title = [lines componentsJoinedByString:@"\n"];
  181. WFCULocationPoint *ponit = [[WFCULocationPoint alloc] initWithCoordinate:locationCoordinate2D andTitle:nil];
  182. [ws.mapView addAnnotation:ponit];
  183. ws.locationPoint = [[WFCULocationPoint alloc] initWithCoordinate:locationCoordinate2D andTitle:title];;
  184. ws.sendButton.enabled = YES;
  185. ws.title = title;
  186. [ws.locationTableView reloadData];
  187. } else {
  188. ws.locationPoint = nil;
  189. ws.sendButton.enabled = NO;
  190. ws.title = @"位置";
  191. }
  192. }];
  193. }
  194. - (void)dismiss:(id)sender {
  195. if (self.navigationController.presentingViewController) {
  196. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  197. }else{
  198. [self.navigationController popViewControllerAnimated:YES];
  199. }
  200. }
  201. #pragma mark - UITableViewDataSource
  202. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  203. return self.marks.count;
  204. }
  205. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  206. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  207. if (!cell) {
  208. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  209. }
  210. NSArray *lines = self.marks[indexPath.row].addressDictionary[@"FormattedAddressLines"];
  211. NSString *title = [lines componentsJoinedByString:@"\n"];
  212. cell.textLabel.text = title;
  213. if (cell.selected) {
  214. cell.imageView.image = [UIImage imageNamed:@"multi_selected"];
  215. } else {
  216. cell.imageView.image = nil;
  217. }
  218. return cell;
  219. }
  220. -(void)dealloc {
  221. [self.locationManager stopUpdatingLocation];
  222. [self.geoCoder cancelGeocode];
  223. }
  224. @end