iOS - 百度地图最基本操作(定位,手势添加自定义标注)

时间:2023-01-23 16:42:41

用 cocoapods 导入百度SDK, 然后开始...


#import "ViewController.h"

#import <BaiduMapAPI_Map/BMKMapView.h>

#import <BaiduMapAPI_Map/BMKPointAnnotation.h>

#import <BaiduMapAPI_Map/BMKPinAnnotationView.h>


@interface ViewController ()<BMKMapViewDelegate,CLLocationManagerDelegate> {

    BMKMapView *_mapView;

}


@property (nonatomic,strong) CLLocationManager *manager;


@end


@implementation ViewController


- (void)viewWillAppear:(BOOL)animated {

    [_mapView viewWillAppear];

    _mapView.delegate = self;

}

- (void)viewWillDisappear:(BOOL)animated {

    [_mapView viewWillDisappear];

    _mapView.delegate = nil;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    _mapView = [[BMKMapView alloc] initWithFrame:self.view.bounds];


    [self.view addSubview:_mapView];

    

    self.manager = [[CLLocationManager alloc] init];

    

    if(![CLLocationManager locationServicesEnabled]){

        NSLog(@"bu ke yong");

        return;

    }

    NSLog(@"ke yong");

    //    CLLocationManager这个类是用来定位的

    self.manager = [[CLLocationManager alloc]init];

    //    间隔多少米去重新定位

    self.manager.distanceFilter =10;

    //   这个属性是设计定位的精确度

    //    kCLLocationAccuracyBest这个是最好的精确度

    self.manager.desiredAccuracy = kCLLocationAccuracyBest;

    //    设置代理

    self.manager.delegate = self;

    

    //    如果当前设备大于等于8.0做一下特殊设置

    if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0)

    {

        [self.manager requestAlwaysAuthorization];

        //        [manager requestWhenInUseAuthorization];

    }

    if ([[UIDevice currentDevice].systemVersion floatValue]>=9.0)

    {

        [self.manager allowsBackgroundLocationUpdates];//允许后台定位更新

    }

    //    开始定位

    [self.manager startUpdatingLocation];

    


    // 手势操作

    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressOption:)];

    gesture.minimumPressDuration = 0.5 // 默认就是 0.5s

    [_mapView addGestureRecognizer:gesture];


}


//这个代理方法是在定位失败的时候会调用

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(nonnull NSError *)error{

    NSLog(@"==error===>>>%@",error.localizedDescription);

}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations{

    CLLocation *location = [locations lastObject];

    

    CLGeocoder *geocoder = [[CLGeocoder alloc]init];

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        //        CLPlacemark这个类里面装的是你当前定位到的那个地点的信息

        CLPlacemark *mark = [placemarks firstObject];

        //        mark.country

        //          mark.location.coordinate

        


        //NSLog(@"======>>%@------%@=====>>>%@",mark.locality,mark.name,mark.addressDictionary);

        

        // 添加一个PointAnnotation

        BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init];

        CLLocationCoordinate2D coor = mark.location.coordinate;

        annotation.coordinate = coor;

        annotation.subtitle = mark.name;

        annotation.title = mark.locality;

        [_mapView addAnnotation:annotation];

        

        _mapView.centerCoordinate = coor;

        

 

    }];

}

// 重写方法

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation

{

    

    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {

       

        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

        newAnnotationView.pinColor = BMKPinAnnotationColorRed;

        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示

        

        // 设置左视图

        UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

        leftImageView.image = [UIImage imageNamed:@"0.png"];

       

        newAnnotationView.leftCalloutAccessoryView = leftImageView;

        

        

        // 设置右视图

        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];

        rightButton.frame = CGRectMake(0, 0, 200, 40);

        [rightButton setTitle:@"H" forState:UIControlStateNormal];

        [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [rightButton addTarget:self action:@selector(sendClick) forControlEvents:UIControlEventTouchUpInside];

       

        

        newAnnotationView.rightCalloutAccessoryView = rightButton;

        

        return newAnnotationView;

    }

    

    return nil;

    

}


#pragma mark - 为地图标注点添加长按手势

- (void)longPressOption:(UILongPressGestureRecognizer *)gesture {

    

    if (gesture.state == UIGestureRecognizerStateRecognized) {


        CGPoint point = [gesture locationInView:_mapView];

        CLLocationCoordinate2D coord = [_mapView convertPoint:point toCoordinateFromView:_mapView];

        

        CLGeocoder *geocoder = [[CLGeocoder alloc]init];

        CLLocation *location = [[CLLocation alloc]initWithLatitude:coord.latitude longitude:coord.longitude];

        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

            

            CLPlacemark *mark = [placemarks lastObject];

            

            BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc] init];

            pointAnnotation.title = mark.locality;

            pointAnnotation.subtitle = mark.name;

            pointAnnotation.coordinate = coord;


            [_mapView addAnnotation:pointAnnotation];


        }];


    }


}

#pragma mark - 标注按钮点击事件

- (void)sendClick {

    NSLog(@"这里是点击所有标注都会调用的方法,,,");

}



基本操作就是这些,后续会更新详细操作的文章。。。
iOS - 百度地图最基本操作(定位,手势添加自定义标注)