首先先導入4個配置文件
#import "AppDelegate.h"#import#import@interface AppDelegate ()
@end
static NSString *APIKey =@"b12a81f5c32c3f061ad8489f079c83d3";
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
? ? [AMapServices sharedServices].apiKey =APIKey;
? ? return YES;
}
導入必要的幾個類庫
視圖頁面:
#import "ViewController.h"
#import <AMapLocationKit/AMapLocationKit.h>
#import <MAMapKit/MAMapKit.h>
#define DefaultLocationTimeout 6
#define DefaultReGeocodeTimeout 3
@interface ViewController ()<MAMapViewDelegate,AMapLocationManagerDelegate>
@property(nonatomic,strong)MAMapView *mapView;
@property(nonatomic,strong)AMapLocationManager *locationManager;
@property(nonatomic,copy)AMapLocatingCompletionBlock completionBlock;
@end
@implementation ViewController
- (void)viewDidLoad {
??? [super viewDidLoad];
??? [self configLocationManager];
??? [self initMapView];
??? [self initCommpleteBlock];
???
}
//定位管理者類
-(void)configLocationManager{
??? self.locationManager=[[AMapLocationManager alloc]init];
??? [self.locationManager setDelegate:self];
//??? 設(shè)置期望定位精度
??? [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//??? 設(shè)置不允許系統(tǒng)暫停定位
??? [self.locationManager setPausesLocationUpdatesAutomatically:NO];
//??? 設(shè)置允許在后臺定位
//??? [self.locationManager setAllowsBackgroundLocationUpdates:YES];
//??? 設(shè)置定位超時時間
??? [self.locationManager setReGeocodeTimeout:DefaultLocationTimeout];
//??? 設(shè)置反地理編碼超時時間
??? [self.locationManager setReGeocodeTimeout:DefaultReGeocodeTimeout];
//??? 設(shè)置開啟虛擬定位風險檢測涉馅,可以根據(jù)需要開啟
??? [self.locationManager setDetectRiskOfFakeLocation:NO];
???
}
//初始化地圖
-(void)initMapView{
??? if (self.mapView==nil) {
??????? self.mapView=[[MAMapView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-128)];
??????? [self.mapView setDelegate:self];
??????? [self.view addSubview:self.mapView];
??? }
}
// Block代碼塊
-(void)initCommpleteBlock{
??? __weak ViewController *weakSelf =self;
??? self.completionBlock = ^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
??????? if (error != nil && error.code == AMapLocationErrorLocateFailed) {
//?????????? 定位錯誤:此時location和regeocode沒有返回值,不進行annotation的添加
??????????? NSLog(@"{%ld - %@};",(long)error.code,error.localizedDescription);
??????????? return ;
??????? }else if (error !=nil && (error.code == AMapLocationErrorReGeocodeFailed ||error.code == AMapLocationErrorTimeOut ||error.code == AMapLocationErrorCannotFindHost ||error.code == AMapLocationErrorNotConnectedToInternet || error.code == AMapLocationErrorCannotConnectToHost)){
//??????????? 逆地理錯誤:在帶逆地理的單詞定位中黄虱,逆地理過程可能發(fā)生錯誤稚矿,此時location有返回值,regeocode無返回值,進行annotation的添加
??????????? NSLog(@":{逆地理錯誤時%ld - %@};",(long)error.code,error.localizedDescription);
??????? }else if (error !=nil && error.code== AMapLocationErrorRiskOfFakeLocation){
//??????????? 存在虛擬定位的風險:此時location和regeocode沒有返回值晤揣,不進行annotation的添加
??????????? NSLog(@"存在虛擬定位失誤%ld-%@",(long)error.code,error.localizedDescription);
??????????? return;
??????? }else{
???????????
??????? }
//??????? 根據(jù)定位信息桥爽,添加annotation
??????? MAPointAnnotation *annotation =[[MAPointAnnotation alloc]init];
??????? [annotation setCoordinate:location.coordinate];
???????
//??????? 有無 逆地理信息。昧识。annotationView的標題顯示的字段不一樣
???????
???????
???????
??????? 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:@"acciracy:%.2fm",location.horizontalAccuracy]];
??????? }
??????? ViewController *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];
}
#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=MAPinAnnotationColorRed;
??????? return annotationView;
??? }
??? return nil;
}
- (IBAction)StopDW:(UIButton *)sender {
//??? 停止定位
??? [self.locationManager stopUpdatingLocation];
??? [self.locationManager setDelegate:nil];
??? [self.mapView removeAnnotations:self.mapView.annotations];
}
- (IBAction)Budaifandili:(UIButton *)sender {
??? [self.mapView removeAnnotations:self.mapView.annotations];
??? //??? 進行單次帶逆地理定位請求
??? [self.locationManager requestLocationWithReGeocode:NO completionBlock:self.completionBlock];
}
- (IBAction)DaiFanDiLi:(UIButton *)sender {
??? [self.mapView removeAnnotations:self.mapView.annotations];
//??? 進行單次帶逆地理定位請求
??? [self.locationManager requestLocationWithReGeocode:YES completionBlock:self.completionBlock];
}