| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- /************************************************************
- * * 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 <CoreLocation/CoreLocation.h>
- #import <MapKit/MapKit.h>
- #import "EaseLocationViewController.h"
- #import "UIViewController+HUD.h"
- #import "EaseLocalDefine.h"
- static EaseLocationViewController *defaultLocation = nil;
- @interface EaseLocationViewController () <MKMapViewDelegate,CLLocationManagerDelegate>
- {
- 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 imageNamed:@"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
|