/************************************************************ * * Hyphenate CONFIDENTIAL * __________________ * Copyright (C) 2016 Hyphenate Inc. All rights reserved. * * NOTICE: All information contained herein is, and remains * the property of Hyphenate Inc. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from Hyphenate Inc. */ #import #import #import "EaseLocationViewController.h" #import "UIViewController+HUD.h" #import "EaseLocalDefine.h" static EaseLocationViewController *defaultLocation = nil; @interface EaseLocationViewController () { MKMapView *_mapView; MKPointAnnotation *_annotation; CLLocationManager *_locationManager; CLLocationCoordinate2D _currentLocationCoordinate; BOOL _isSendLocation; } @property (strong, nonatomic) NSString *addressString; @end @implementation EaseLocationViewController @synthesize addressString = _addressString; - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { _isSendLocation = YES; } return self; } - (instancetype)initWithLocation:(CLLocationCoordinate2D)locationCoordinate { self = [super initWithNibName:nil bundle:nil]; if (self) { _isSendLocation = NO; _currentLocationCoordinate = locationCoordinate; } return self; } + (instancetype)defaultLocation { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ defaultLocation = [[EaseLocationViewController alloc] initWithNibName:nil bundle:nil]; }); return defaultLocation; } - (void)viewDidLoad { [super viewDidLoad]; self.title = NSEaseLocalizedString(@"location.messageType", @"location message"); UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)]; [backButton setImage:[UIImage easeImageNamed:@"EaseUIResource.bundle/back"] forState:UIControlStateNormal]; [backButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; [self.navigationItem setLeftBarButtonItem:backItem]; _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; _mapView.delegate = self; _mapView.mapType = MKMapTypeStandard; _mapView.zoomEnabled = YES; [self.view addSubview:_mapView]; if (_isSendLocation) { _mapView.showsUserLocation = YES;//显示当前位置 UIButton *sendButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)]; [sendButton setTitle:NSEaseLocalizedString(@"send", @"Send") forState:UIControlStateNormal]; sendButton.accessibilityIdentifier = @"send_location"; [sendButton setTitleColor:[UIColor colorWithRed:32 / 255.0 green:134 / 255.0 blue:158 / 255.0 alpha:1.0] forState:UIControlStateNormal]; [sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; [sendButton addTarget:self action:@selector(sendLocation) forControlEvents:UIControlEventTouchUpInside]; [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:sendButton]]; self.navigationItem.rightBarButtonItem.enabled = NO; [self startLocation]; } else{ [self removeToLocation:_currentLocationCoordinate]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } #pragma mark - MKMapViewDelegate - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { __weak typeof(self) weakSelf = self; CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *array, NSError *error) { if (!error && array.count > 0) { CLPlacemark *placemark = [array objectAtIndex:0]; weakSelf.addressString = placemark.name; [self removeToLocation:userLocation.coordinate]; } }]; } - (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error { [self hideHud]; if (error.code == 0) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:[error.userInfo objectForKey:NSLocalizedRecoverySuggestionErrorKey] delegate:nil cancelButtonTitle:NSEaseLocalizedString(@"ok", @"OK") otherButtonTitles:nil, nil]; [alertView show]; } } - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { switch (status) { case kCLAuthorizationStatusNotDetermined: if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [_locationManager requestWhenInUseAuthorization]; } break; case kCLAuthorizationStatusDenied: { } default: break; } } #pragma mark - public /*! @method @brief 开启定位 @discussion @result */ - (void)startLocation { if([CLLocationManager locationServicesEnabled]){ _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; _locationManager.distanceFilter = 5; _locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//kCLLocationAccuracyBest; if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { [_locationManager requestWhenInUseAuthorization]; } } if (_isSendLocation) { self.navigationItem.rightBarButtonItem.enabled = NO; } [self showHudInView:self.view hint:NSEaseLocalizedString(@"location.ongoning", @"locating...")]; } /*! @method @brief 地图添加大头针 @discussion @param coords 位置信息 @result */ -(void)createAnnotationWithCoords:(CLLocationCoordinate2D)coords { if (_annotation == nil) { _annotation = [[MKPointAnnotation alloc] init]; } else{ [_mapView removeAnnotation:_annotation]; } _annotation.coordinate = coords; [_mapView addAnnotation:_annotation]; } /*! @method @brief 大头针移动到指定位置 @discussion @param locationCoordinate 指定位置 @result */ - (void)removeToLocation:(CLLocationCoordinate2D)locationCoordinate { [self hideHud]; _currentLocationCoordinate = locationCoordinate; float zoomLevel = 0.01; MKCoordinateRegion region = MKCoordinateRegionMake(_currentLocationCoordinate, MKCoordinateSpanMake(zoomLevel, zoomLevel)); [_mapView setRegion:[_mapView regionThatFits:region] animated:YES]; if (_isSendLocation) { self.navigationItem.rightBarButtonItem.enabled = YES; } [self createAnnotationWithCoords:_currentLocationCoordinate]; } - (void)sendLocation { if (_delegate && [_delegate respondsToSelector:@selector(sendLocationLatitude:longitude:andAddress:)]) { [_delegate sendLocationLatitude:_currentLocationCoordinate.latitude longitude:_currentLocationCoordinate.longitude andAddress:_addressString]; } [self.navigationController popViewControllerAnimated:YES]; } @end