Objective-C之定位&地图

本文介绍了在Objective-C中如何使用MapKit进行地图操作,并展示了检测定位服务、启动定位及反地理编码和地理编码的代码片段。

转载注明出处,不做抄袭党。

首先要记得调用MapKit

#import <MapKit/MapKit.h>

创建视图就不说了,下面介绍检测定位服务是否开启

 if ([CLLocationManager locationServicesEnabled]) {
        _locationManager = [[CLLocationManager alloc] init];
        if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            [_locationManager requestWhenInUseAuthorization];
        }
        //设置代理
        [_locationManager setDelegate:self];
        //设置精度
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        //设置距离筛选
        [_locationManager setDistanceFilter:10];
        //开始定位
        [_locationManager startUpdatingLocation];



    }else{
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil
                                                             message:@"定位没有开启"
                                                            delegate:nil
                                                   cancelButtonTitle:@"确定"
                                                   otherButtonTitles:nil, nil];
        [alertView show];
    }


}

定位服务开启后我们便可以开始定位

//定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation * location = locations.lastObject;
    [self reverseGeocoder:location];
        MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), MKCoordinateSpanMake(0.1, 0.1));

        [_mapView setRegion:[_mapView regionThatFits:coordinateRegion] animated:YES];
}



//定位失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

}

反地理编码代码如下

- (void)reverseGeocoder:(CLLocation *)currentLocation {
    CLGeocoder * geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if(error || placemarks.count == 0){
            NSLog(@"error");
        }else{
            CLPlacemark * placemark = placemarks.firstObject;

            //
            MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude), MKCoordinateSpanMake(0.1, 0.1));

            [_mapView setRegion:[_mapView regionThatFits:coordinateRegion] animated:YES];

            MKPointAnnotation * pointAnnotation = [[MKPointAnnotation alloc] init];
            [pointAnnotation setTitle:placemark.name];
            [pointAnnotation setCoordinate:CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)];
            [_mapView addAnnotation:pointAnnotation];

            NSLog(@"placemark:%@",[[placemark addressDictionary] objectForKey:@"city"]);
        }
    }];
}

地理编码代码如下

- (void)geocoder:(NSString *)str {
    CLGeocoder * geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:str completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (error || placemarks.count == 0) {
            NSLog(@"error");
        }else{
            CLPlacemark * placemark = placemarks.firstObject;

            MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude), MKCoordinateSpanMake(0.1, 0.1));

            [_mapView setRegion:[_mapView regionThatFits:coordinateRegion] animated:YES];

            MKPointAnnotation * pointAnnotation = [[MKPointAnnotation alloc] init];
            [pointAnnotation setTitle:placemark.name];
            [pointAnnotation setCoordinate:CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)];
            [_mapView addAnnotation:pointAnnotation];

        }
    }];
}

今日手打代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值