iOS地图和定位

  • 导入地图框架
#import 
#import 

1、定位

//CLLocationManager定位器
//1 判断设备是否支持定位服务
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"不支持此设备");
        return;
    }
    //2 创建定位服务
    _locationManager = [[CLLocationManager alloc] init];
    //3 请求用户授权(使用期间)  在info.plist文件添加的字段必须与设置的字段一致
    [_locationManager requestWhenInUseAuthorization];

    //4 配置定位服务
    //4.1 设置定位精度
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //4.2 设置过滤距离(以用户位置方圆100米为半径,实际位置超过该范围才会更新位置。否则不更新)
    _locationManager.distanceFilter = 100;
    //4.3 设置代理
    _locationManager.delegate = self;

    //5 开始定位,定位为耗时操作。定位成功后一定关闭定位。
    [_locationManager startUpdatingLocation];
// 在模拟器上定位失败的解决方法
//1 重置模拟器
//2 模拟器设置。debug - location - apple 和 customer 之间切换

2、地图标点

//字符转化
    CGFloat latitude = [dic[@"LATITUDE"] floatValue];
    CGFloat longtitude = [dic[@"LONGITUDE"] floatValue];

    //设置地图显示的中心点及缩放比例
    CLLocationCoordinate2D center = {latitude,longtitude};
    MKCoordinateSpan span;

    span.latitudeDelta = 1;
    span.longitudeDelta = 1;

    MKCoordinateRegion region = {center,span};

    [self.mapView setRegion:region animated:YES];


        MKPointAnnotation *anno = [[MKPointAnnotation alloc] init];
        anno.title = dic[@"NAME"];
        anno.subtitle = dic[@"ADDRESS"];
        anno.coordinate = center;

        //调用此方法在地图上加坐标点
        [self.mapView addAnnotation:anno];

3、根据地名获取经纬度

- (void)showLatitudeFromCityName:(NSString *)cityName {

       __block  NSDictionary *dic = [NSDictionary dictionary];

    CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
    [myGeocoder geocodeAddressString:cityName completionHandler:^(NSArray *placemarks, NSError *error) {
        if ([placemarks count] > 0 && error == nil)
        {
            CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];


            dic = @{@"LONGITUDE":@(firstPlacemark.location.coordinate.longitude),
                    @"LATITUDE":@(firstPlacemark.location.coordinate.latitude)
                    };

            [self showPointOfLatitude:dic];
        }
        else if ([placemarks count] == 0 && error == nil)
        {
            NSLog(@"Found no placemarks.");
        }
        else if (error != nil)
        {
            NSLog(@"An error occurred = %@", error);
        }
    }];
}

4、反向解析

(1)与长按手势进行配合(手势添加到地图上)
//长按手势
    UILongPressGestureRecognizer *gest = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

    [self.mapView addGestureRecognizer:gest];

(2)根据选中的点,获取相关点
- (void)longPressAction:(UILongPressGestureRecognizer *)sender{

    //获取触摸的点
    CGPoint point = [sender locationInView:self.mapView];
    //通过触摸的点获取经纬度
    CLLocationCoordinate2D coord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    //组装参数
    CLLocation *location = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
    //获取信息
    [geo reverseGeocodeLocation:location completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
        //获取位置
        if (placemarks.count != 0) {
            CLPlacemark *placeMark = placemarks.firstObject;

            NSArray *addressArray = placeMark.addressDictionary[@"FormattedAddressLines"];

            //拼接地址
            NSMutableString *address = [[NSMutableString alloc] init];

            for (int i = 0; i < addressArray.count; i ++) {
                [address appendString:addressArray[i]];
            }

            MKPointAnnotation *anno = [[MKPointAnnotation alloc] init];
            anno.title = placeMark.name;
            anno.subtitle = address;
            anno.coordinate = coord;

            [self.mapView addAnnotation:anno];

        }

    }];
}

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