地图Map

2018-06-13  本文已影响0人  rainbow_H

- (void)viewDidLoad {

    [super viewDidLoad];

    /**创建地图*/

    [self creatMap];

    /**定位当前*/  // 模拟器上无法定位

    [self selfLocation];

}

#pragma mark定位自己

- (void)selfLocation{

    // 创建定位对象

    _locationManager = [[CLLocationManager alloc]init];

    // 设置定位属性

    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    // 设置定位更新距离

    _locationManager.distanceFilter = 10.0;

    // 绑定定位委托

    _locationManager.delegate = self;

    //取出当前应用的定位服务状态(枚举值)

    CLAuthorizationStatus status =[CLLocationManager authorizationStatus];

    //如果未授权则请求

    if(status==kCLAuthorizationStatusNotDetermined) {

        [_locationManager requestAlwaysAuthorization];

    }

    // 开始定位

    [_locationManager startUpdatingLocation];

}

#pragma mark CLLocationManagerDelegate

// 定位后的返回结果

- (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations{

    //locations数组中存储的是CLLocation

    //遍历数组取出坐标--》如果不需要也可以不遍历

    CLLocation*location =[locationsfirstObject];

     MKCoordinateRegion region =MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.01,0.01));

    [_mapView setRegion:region animated:YES];

    // 创建大头针

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

    // 设置经纬度

    point.coordinate= location.coordinate;

    // 设置主标题

    point.title = @"The location of the I";

    // 设置副标题

    point.subtitle=@"Hello Map";

    // 将大头针添加到地图上

    [_mapViewaddAnnotation:point];

}

#pragma mark创建地图

-(void)creatMap{

    // 实例化

    _mapView = [[MKMapView alloc]initWithFrame:self.view.frame];

    // 是否可以缩放

    _mapView.zoomEnabled = YES;

    // 是否可以滚动

    _mapView.scrollEnabled = YES;

    // 是否可以旋转

    _mapView.rotateEnabled = YES;

    // 是否显示3D

    _mapView.pitchEnabled = YES;

    // 是否显示指南针

     _mapView.showsCompass = YES;

     // 是否显示比例尺

     _mapView.showsScale = YES;

     // 是否显示交通

    _mapView.showsTraffic = YES;

     // 是否显示建筑物

     _mapView.showsBuildings = YES;

     //绑定委托

    _mapView.delegate=self;

    //添加到界面

    [self.view addSubview:_mapView];

    //添加手势

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

     //添加到地图上

     [_mapView addGestureRecognizer:longPress];

    //创建UISegmentedControl

    NSArray*mapTypeArray =@[@"标准",@"卫星地图",@"混合"];

    UISegmentedControl *segment =[[UISegmentedControl alloc] initWithItems:mapTypeArray];

     segment.frame=CGRectMake(50,50,300,50);

     [_mapViewaddSubview:segment];

    segment.selectedSegmentIndex=0;

}

- (void)longPress:(UILongPressGestureRecognizer *)sender{

    // 判断是否长按

    if (sender.state != UIGestureRecognizerStateBegan) {

        return;

    }

}

上一篇 下一篇

猜你喜欢

热点阅读