地图定位

#import "ViewController.h"
#import  //地图
#import  // 定位
@interface ViewController ()
{
    CLGeocoder *_geo;//根据经纬度,反向解析地址
}
@property (nonatomic, strong) MKMapView *mapview;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化 geo 对象
    _geo = [[CLGeocoder alloc]init];
 
    self.mapview = [[MKMapView alloc]initWithFrame:self.view.bounds];
    self.mapview.delegate = self;
    [self showPointOfLatitude:30.659462 longitude:104.065735];
    
    [self.view addSubview:self.mapview];
    
    UILongPressGestureRecognizer *gest = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    [self.mapview addGestureRecognizer:gest];
}
- (void)showPointOfLatitude:(CGFloat)latitude longitude:(CGFloat)longitude{
    //中心点(结构体)
    CLLocationCoordinate2D center = {latitude,longitude};
    //缩放比例(结构体)
    MKCoordinateSpan span ;
    span.latitudeDelta = 0.005;
    span.longitudeDelta = 0.005;
    MKCoordinateRegion region = {center,span};
    //设置地图的中心点和缩放比例 setRegion
    [self.mapview setRegion:region animated:YES];
    
#pragma mark -- 设置点的信息
    MKPointAnnotation *anno = [[MKPointAnnotation alloc]init];
    anno.title = @"四川科技馆";
    anno.subtitle = @"天府广场附近的一点";
    anno.coordinate = center;
    //调用addAnnotation 方法去给地图上加点
    [self.mapview addAnnotation:anno];
}
#pragma mark -- 点击事件
- (void)detailButtonClick:(UIButton *)sender {
    
}
//长按手势
- (void)longPressAction:(UILongPressGestureRecognizer *)sender {
    //获取常按点
    CGPoint point = [sender locationInView:self.mapview];
    //通过地图方法 convertPoint: toCoordinateFromView: 把点转换成经纬度
    CLLocationCoordinate2D coord = [self.mapview convertPoint:point toCoordinateFromView:self.mapview];
    
    //组装CLLocation 参数
    CLLocation *location = [[CLLocation alloc]initWithLatitude:coord.latitude longitude:coord.longitude];
    
    [_geo  reverseGeocodeLocation:location completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0 && error == nil) {
            CLPlacemark *placemark = placemarks[0];
            NSArray *addArray = placemark.addressDictionary[@"FormattedAddressLines"];
            //讲一个详细的地址转换成字符串
            NSMutableString * address = [[NSMutableString alloc]init];
            for (int i = 0; i < addArray.count; i ++) {
                [address appendString: addArray[i]];
            }
            
            //设置点
            MKPointAnnotation *anno = [[MKPointAnnotation alloc]init];
            anno.title = placemark.name;
            anno.subtitle = address;
            anno.coordinate = coord;
            [self.mapview addAnnotation:anno];
            
        }
    }];
}
#pragma mark -- MKMapViewDelegate 协议方法
//自定义大头针的方法
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation{
    static NSString *annoID = @"annoId";
    MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:annoID];
    if (!annoView) {
        annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annoID];
    }
    //设置大头针的头
    annoView.image = [UIImage imageNamed:@"F9F748E09B377F4B56DDFE0977FA40DE.gif"];
    //允许点击大头针弹窗显示详情
    annoView.canShowCallout = YES;
    UIButton *detailButton= [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [detailButton addTarget:self action:@selector(detailButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    annoView.rightCalloutAccessoryView = detailButton;
    
    
    
    //返回自定义大头针
    return annoView;
}

你可能感兴趣的:(地图定位)