地图

#import "ViewController.h"
//导入头文件
#import <MapKit/MapKit.h>

#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate,MKMapViewDelegate>
@property (nonatomic, strong) CLLocationManager *manager;
@property (nonatomic, strong) MKMapView *mapView;//地图
@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //地图
    [self initMapView];



    NSArray *arr = @[@"移动",@"回到用户位置"];
    for (NSInteger i = 0; i < arr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(100*i, 100, 100, 30);
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//        button.backgroundColor = [UIColor redColor];
        button.tag = 100+i;
        [self.view addSubview:button];
    }
    //长按手势
    [self createLongPress];
}
- (void)btnClick:(UIButton *)button {
    switch (button.tag) {
        case 100:
        {
            CLLocationCoordinate2D coordinate = self.mapView.region.center;
            coordinate.latitude += 0.01;
            coordinate.longitude += 0.01;
            //修改区域中心点
            [self.mapView setCenterCoordinate:coordinate animated:YES];
        }
            break;
        case 101:
        {
            //获取当前用户的位置
            CLLocation *location =  self.mapView.userLocation.location;
            //回到用户位置
            [self.mapView setCenterCoordinate:location.coordinate animated:YES];
        }
            break;

        default:
            break;
    }
}
#pragma mark - 定位
- (void)initManager {
    if (self.manager) {
        return;
    }
    self.manager = [[CLLocationManager alloc] init];
#ifdef __IPHONE_8_0 //判断版本
    if ([self.manager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        //有 就调用
        [self.manager performSelector:@selector(requestAlwaysAuthorization)];
    }
    self.manager.delegate = self;
#endif
}
//定位 位置改变之后调用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

}

- (void)initMapView {
    [self initManager];//需要定位 就调用

    //实例化 地图
    self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    self.mapView.mapType = MKMapTypeStandard;
    //设置地图显示的 区域 (给一个中心位置)
    // 给一个 经纬度 和 缩放比例(0.01---0.05)
    //34.77274892, 113.67591140
    self.mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(34.77274892, 113.67591140),MKCoordinateSpanMake(0.01, 0.01));
    //是否显示 用户位置
    self.mapView.showsUserLocation = YES;

    //设置代理
    self.mapView.delegate = self;//可以操作点标注

    //粘贴地图
    [self.view addSubview:self.mapView];

    //增加大头针
    [self createAnnotation];

}

#pragma mark - 创建点标注
- (void)createAnnotation {
    //实例化一个点标注 (点标注(大头针)数据)
    //这个对象实际上就是一个数据模型
    MKPointAnnotation *annotation1 = [[MKPointAnnotation alloc] init];
    //设置经纬度
    annotation1.coordinate = CLLocationCoordinate2DMake(34.77274892, 113.67591140);
    //主标题
    annotation1.title = [NSString stringWithFormat:@"%f", annotation1.coordinate.latitude];
    //副标题
    annotation1.subtitle = [NSString stringWithFormat:@"%f", annotation1.coordinate.longitude];
    //增加到 地图上
    [self.mapView addAnnotation:annotation1];//一旦增加点标注 那么立即会回调显示大头针视图的方法


    MKPointAnnotation *annotation2 = [[MKPointAnnotation alloc] init];

    annotation2.coordinate = CLLocationCoordinate2DMake(34.77274892, 113.67591140+0.003);
    annotation2.title = [NSString stringWithFormat:@"%f", annotation2.coordinate.latitude];
    annotation2.subtitle = [NSString stringWithFormat:@"%f", annotation2.coordinate.latitude];
    [self.mapView addAnnotation:annotation2];

}
/*
 地图mapView                      -》tableView
 大头针/点标注视图MKAnnotationView---》tableView上的Cell
 MKPointAnnotation 大头针数据      -》数据模型
 */
#pragma mark - MapViewDelegate
// 当向地图上增加点标注数据的时候 调用下面的方法显示点标注视图

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    //annotation 就是 向地图上增加的点标注数据
    //根据这个数据 显示指定的点标注视图
    if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
        //判断是哪一类点标注数据
        //创建 点标注视图 (采用复用机制)
        //队列获取空闲的
        MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MKPinAnnotationView"];
        if (pinView == nil) {
            //没有那么创建新的
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MKPinAnnotationView"];
        }
        //设置属性
        //是否显示气泡
        pinView.canShowCallout = YES;
        //是否有掉落的动画
        pinView.animatesDrop = YES;//(点标注视图子类MKPinAnnotationView才可以设置)
        //设置大头针视图的颜色  红  绿  紫三种
        pinView.pinColor =  MKPinAnnotationColorPurple;

        //设置气泡的左右侧附件
        UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        redView.backgroundColor = [UIColor redColor];
        pinView.leftCalloutAccessoryView = redView;

        UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
        button.frame = CGRectMake(0, 0, 30, 30);
        //气泡附件 如果是UIControl的子类 不需要再增加事件,有一个协议的方法可以替代
        //[button addTarget:<#(id)#> action:<#(SEL)#> forControlEvents:<#(UIControlEvents)#>];
        pinView.rightCalloutAccessoryView = button;
        return pinView;//返回对象地址
    }

    return nil;
}
//点击 气泡附件的时候调用
//必须要保证 附件是UIControl的子类
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    NSLog(@"气泡附件被点击");
}
//选中/点击 点标注视图的时候调用
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    NSLog(@"大头针视图被点击/选中");
}
#pragma mark - 长按添加点标注
- (void)createLongPress {
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.mapView addGestureRecognizer: longPress];
}
- (void)longPress:(UILongPressGestureRecognizer *)press {
    //判断 状态 长按有 两个状态 开始状态 长按结束状态
    if (press.state == UIGestureRecognizerStateEnded) {
        //长按结束增加大头针
        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
        //获取点击的 坐标 -->相对地图的视图坐标
        CGPoint point = [press locationInView:self.mapView];
        //把 这个 坐标 转化为 相对于地图的 真正经纬度
        annotation.coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];


        //实际上应该根据经纬度 进行地理反编码得到 地址 作为大头针数据
        //下面我把这个数据写固定数据
        annotation.title = [NSString stringWithFormat:@"%f", annotation.coordinate.latitude];
        annotation.subtitle = [NSString stringWithFormat:@"%f", annotation.coordinate.longitude];
//        annotation.title = @"";
//        annotation.subtitle = @"";

        //增加到地图上
        [self.mapView addAnnotation:annotation];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值