关于百度地图定位
首先先导入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];
// 设置期望定位精度
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
// 设置不允许系统暂停定位
[self.locationManager setPausesLocationUpdatesAutomatically:NO];
// 设置允许在后台定位
// [self.locationManager setAllowsBackgroundLocationUpdates:YES];
// 设置定位超时时间
[self.locationManager setReGeocodeTimeout:DefaultLocationTimeout];
// 设置反地理编码超时时间
[self.locationManager setReGeocodeTimeout:DefaultReGeocodeTimeout];
// 设置开启虚拟定位风险检测,可以根据需要开启
[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)){
// 逆地理错误:在带逆地理的单词定位中,逆地理过程可能发生错误,此时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{
}
// 根据定位信息,添加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];
}