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

来源:互联网 发布:js如何实现继承 编辑:程序博客网 时间:2024/05/16 15:29

用 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 {

    [_mapViewviewWillAppear];

    _mapView.delegate =self;

}

- (void)viewWillDisappear:(BOOL)animated {

    [_mapViewviewWillDisappear];

    _mapView.delegate =nil;

}

- (void)viewDidLoad {

    [superviewDidLoad];

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

    

    _mapView = [[BMKMapViewalloc] initWithFrame:self.view.bounds];


    [self.viewaddSubview:_mapView];

    

    self.manager = [[CLLocationManageralloc] init];

    

    if(![CLLocationManagerlocationServicesEnabled]){

        NSLog(@"bu ke yong");

        return;

    }

    NSLog(@"ke yong");

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

    self.manager = [[CLLocationManageralloc]init];

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

    self.manager.distanceFilter =10;

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

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

    self.manager.desiredAccuracy = kCLLocationAccuracyBest;

    //    设置代理

    self.manager.delegate =self;

    

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

    if ([[UIDevicecurrentDevice].systemVersionfloatValue]>=8.0)

    {

        [self.managerrequestAlwaysAuthorization];

        //        [manager requestWhenInUseAuthorization];

    }

    if ([[UIDevicecurrentDevice].systemVersionfloatValue]>=9.0)

    {

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

    }

    //    开始定位

    [self.managerstartUpdatingLocation];

    


    // 手势操作

    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPressOption:)];

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

    [_mapViewaddGestureRecognizer:gesture];


}


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

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

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

}

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

    CLLocation *location = [locationslastObject];

    

    CLGeocoder *geocoder = [[CLGeocoderalloc]init];

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

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

        CLPlacemark *mark = [placemarksfirstObject];

        //        mark.country

        //          mark.location.coordinate

        


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

        

        // 添加一个PointAnnotation

        BMKPointAnnotation *annotation = [[BMKPointAnnotationalloc]init];

        CLLocationCoordinate2D coor = mark.location.coordinate;

        annotation.coordinate = coor;

        annotation.subtitle = mark.name;

        annotation.title = mark.locality;

        [_mapViewaddAnnotation:annotation];

        

        _mapView.centerCoordinate = coor;

        

 

    }];

}

// 重写方法

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

{

    

    if ([annotationisKindOfClass:[BMKPointAnnotationclass]]) {

       

        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationViewalloc] initWithAnnotation:annotationreuseIdentifier:@"myAnnotation"];

        newAnnotationView.pinColor =BMKPinAnnotationColorRed;

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

        

        // 设置左视图

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

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

       

        newAnnotationView.leftCalloutAccessoryView = leftImageView;

        

        

        // 设置右视图

        UIButton *rightButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

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

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

        [rightButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

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

       

        

        newAnnotationView.rightCalloutAccessoryView = rightButton;

        

        return newAnnotationView;

    }

    

    returnnil;

    

}


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

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

    

    if (gesture.state ==UIGestureRecognizerStateRecognized) {


        CGPoint point = [gesturelocationInView:_mapView];

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

        

        CLGeocoder *geocoder = [[CLGeocoderalloc]init];

        CLLocation *location = [[CLLocationalloc]initWithLatitude:coord.latitudelongitude:coord.longitude];

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

            

            CLPlacemark *mark = [placemarkslastObject];

            

            BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotationalloc] init];

            pointAnnotation.title = mark.locality;

            pointAnnotation.subtitle = mark.name;

            pointAnnotation.coordinate = coord;


            [_mapViewaddAnnotation:pointAnnotation];


        }];


    }


}

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

- (void)sendClick {

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

}



基本操作就是这些,后续会更新详细操作的文章。。。


0 0