Objective-C之定位&地图

来源:互联网 发布:淘宝秒杀群运营系统 编辑:程序博客网 时间:2024/06/06 12:28

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

首先要记得调用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];        }    }];}

今日手打代码

0 0
原创粉丝点击