定位知识:
@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) AMapLocationManager *locationManager;
@interface SingleLocationViewController () <MAMapViewDelegate, AMapLocationManagerDelegate>
self.locationManager = [[AMapLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
[self.locationManager setLocationTimeout:DefaultLocationTimeout];
[self.locationManager setReGeocodeTimeout:DefaultReGeocodeTimeout];
[self.locationManager stopUpdatingLocation];
[self.locationManager setDelegate:nil];
[self.mapView removeAnnotations:self.mapView.annotations];
[self.mapView removeAnnotations:self.mapView.annotations];
[self.locationManager requestLocationWithReGeocode:NO completionBlock:self.completionBlock];
[self.mapView removeAnnotations:self.mapView.annotations];
[self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
if (annotationView == nil)
{
annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
}
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
annotationView.draggable = NO;
annotationView.pinColor = MAPinAnnotationColorPurple;
return annotationView;
}
return nil;
}
@property (nonatomic, copy) AMapLocatingCompletionBlock completionBlock;
- (void)initCompleteBlock
{
__weak SingleLocationViewController *weakSelf = self;
self.completionBlock = ^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error)
{
if (error)
{
NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
if (error.code == AMapLocationErrorLocateFailed)
{
return;
}
}
if (location)
{
MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
[annotation setCoordinate:location.coordinate];
if (regeocode)
{
[annotation setTitle:[NSString stringWithFormat:@"%@", regeocode.formattedAddress]];
[annotation setSubtitle:[NSString stringWithFormat:@"%@-%@-%.2fm", regeocode.citycode, regeocode.adcode, location.horizontalAccuracy]];
}
else
{
[annotation setTitle:[NSString stringWithFormat:@"lat:%f;lon:%f;", location.coordinate.latitude, location.coordinate.longitude]];
[annotation setSubtitle:[NSString stringWithFormat:@"accuracy:%.2fm", location.horizontalAccuracy]];
}
SingleLocationViewController *strongSelf = weakSelf;
[strongSelf addAnnotationToMapView:annotation];
}
};
}
- (void)addAnnotationToMapView:(id<MAAnnotation>)annotation
{
[self.mapView addAnnotation:annotation];
[self.mapView selectAnnotation:annotation animated:YES];
[self.mapView setZoomLevel:15.1 animated:NO];
[self.mapView setCenterCoordinate:annotation.coordinate animated:YES];
}
- (void)locAction
{
[self.mapView removeAnnotations:self.mapView.annotations];
[self.locationManager requestLocationWithReGeocode:NO completionBlock:self.completionBlock];
}
SingleLocationViewController.h
#import <UIKit/UIKit.h>
#import <MAMapKit/MAMapKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
@interface SingleLocationViewController : UIViewController
@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) AMapLocationManager *locationManager;
@end
SingleLocationViewController.m
#import "SingleLocationViewController.h"
#define DefaultLocationTimeout 6
#define DefaultReGeocodeTimeout 3
@interface SingleLocationViewController () <MAMapViewDelegate, AMapLocationManagerDelegate>
@property (nonatomic, copy) AMapLocatingCompletionBlock completionBlock;
@end
@implementation SingleLocationViewController
#pragma mark - Action Handle
- (void)configLocationManager
{
self.locationManager = [[AMapLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
[self.locationManager setLocationTimeout:DefaultLocationTimeout];
[self.locationManager setReGeocodeTimeout:DefaultReGeocodeTimeout];
}
- (void)cleanUpAction
{
[self.locationManager stopUpdatingLocation];
[self.locationManager setDelegate:nil];
[self.mapView removeAnnotations:self.mapView.annotations];
}
- (void)reGeocodeAction
{
[self.mapView removeAnnotations:self.mapView.annotations];
[self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
}
- (void)locAction
{
[self.mapView removeAnnotations:self.mapView.annotations];
[self.locationManager requestLocationWithReGeocode:NO completionBlock:self.completionBlock];
}
#pragma mark - Initialization
- (void)initCompleteBlock
{
__weak SingleLocationViewController *weakSelf = self;
self.completionBlock = ^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error)
{
if (error)
{
NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
if (error.code == AMapLocationErrorLocateFailed)
{
return;
}
}
if (location)
{
MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
[annotation setCoordinate:location.coordinate];
if (regeocode)
{
[annotation setTitle:[NSString stringWithFormat:@"%@", regeocode.formattedAddress]];
[annotation setSubtitle:[NSString stringWithFormat:@"%@-%@-%.2fm", regeocode.citycode, regeocode.adcode, location.horizontalAccuracy]];
}
else
{
[annotation setTitle:[NSString stringWithFormat:@"lat:%f;lon:%f;", location.coordinate.latitude, location.coordinate.longitude]];
[annotation setSubtitle:[NSString stringWithFormat:@"accuracy:%.2fm", location.horizontalAccuracy]];
}
SingleLocationViewController *strongSelf = weakSelf;
[strongSelf addAnnotationToMapView:annotation];
}
};
}
- (void)addAnnotationToMapView:(id<MAAnnotation>)annotation
{
[self.mapView addAnnotation:annotation];
[self.mapView selectAnnotation:annotation animated:YES];
[self.mapView setZoomLevel:15.1 animated:NO];
[self.mapView setCenterCoordinate:annotation.coordinate animated:YES];
}
- (void)initMapView
{
if (self.mapView == nil)
{
self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
[self.mapView setDelegate:self];
[self.view addSubview:self.mapView];
}
}
- (void)initToolBar
{
UIBarButtonItem *flexble = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *reGeocodeItem = [[UIBarButtonItem alloc] initWithTitle:@"带逆地理定位"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(reGeocodeAction)];
UIBarButtonItem *locItem = [[UIBarButtonItem alloc] initWithTitle:@"不带逆地理定位"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(locAction)];
self.toolbarItems = [NSArray arrayWithObjects:flexble, reGeocodeItem, flexble, locItem, flexble, nil];
}
- (void)initNavigationBar
{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Clean"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(cleanUpAction)];
}
#pragma mark - Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self initToolBar];
[self initNavigationBar];
[self initMapView];
[self initCompleteBlock];
[self configLocationManager];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.toolbar.translucent = YES;
self.navigationController.toolbarHidden = NO;
}
- (void)dealloc
{
[self cleanUpAction];
self.completionBlock = nil;
}
#pragma mark - MAMapView Delegate
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
if (annotationView == nil)
{
annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
}
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
annotationView.draggable = NO;
annotationView.pinColor = MAPinAnnotationColorPurple;
return annotationView;
}
return nil;
}
@end