ios 自带地图简单使用

本文详细介绍如何在iOS应用中集成地图功能,包括设置plist配置文件,导入地图应用框架,定义地图和定位属性,重写属性的getter方法,设置用户授权,遵守协议并实现代理方法以获取和显示当前位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 设置 plist 配置文件

  • 导入两个地图应用框架
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
  • 定义地图和地位属性
@property (nonatomic , strong) MKMapView *mapView;        ///<地图
@property (nonatomic , strong) CLLocationManager *locManager;   ///<获得定位
  • 重写两个属性的 getter 方法
///重写 getter
- (CLLocationManager *)locManager{
    
    if (!_locManager) {
        
        _locManager = [[CLLocationManager alloc]init];
        _locManager.activityType = CLActivityTypeFitness;   ///<步行导航
        _locManager.delegate = self;
    }
    
    return _locManager;
}

- (MKMapView *)mapView{
    
    if (!_mapView) {
        
        self.mapView = [[MKMapView alloc]initWithFrame:self.view.frame];
        self.mapView.delegate = self;
        //设置地图类型
        _mapView.mapType = MKMapTypeStandard;   //平面地图
    }
    
    return _mapView;
}
  • 在viewDidLoad 中设置用户授权
//申请用户授权
[self.locManager requestWhenInUseAuthorization];
  • 遵守协议
<CLLocationManagerDelegate ,MKMapViewDelegate>
  • 得到当前位置触发事件
 [_locManager startUpdatingLocation];    //开始更新当前位置信息
  • CLLocationManagerDelegate代理方法中获取当前位置

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    //得到当前位置
    CLLocation *currentLocation = locations.lastObject;
    
    //位置    ,此对象已经采用了 MK 协议
    MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
    point.coordinate = currentLocation.coordinate;
    
    point.title = @"当前位置";
    //地址解析
    CLGeocoder *gecoder = [[CLGeocoder alloc]init];
    
    [gecoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *place = placemarks.lastObject;
        
        point.subtitle = place.name;
    }];
    
    //添加大头针
    [_mapView addAnnotation:point];
    
    //需要将地图的显示区域变小
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(currentLocation.coordinate, 800, 800);
    [_mapView setRegion:region animated:YES];
}
  • MKMapViewDelegate代理设置描边

//设置锚点样式
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    
    MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
    
    if (!pin) {
        
        pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"];
    }
    
    pin.pinTintColor = [MKPinAnnotationView purplePinColor];
    pin.animatesDrop = YES;
    pin.canShowCallout = YES;
    
    return pin;
}
  • 定位自定义的位置(您输入的位置) 点击事件
//传递过来省会的字符串做地址解析
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    [geocoder geocodeAddressString:@"河北省邯郸市永年区" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *place = placemarks.lastObject;
        CLLocationCoordinate2D coord = place.location.coordinate;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //位置    ,此对象已经采用了 MK 协议
            MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
            point.coordinate = coord;
            //添加大头针
            [self.mapView addAnnotation:point];
            
            point.title = @"当前位置";
            
            [geocoder reverseGeocodeLocation:place.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
                
                CLPlacemark *place = placemarks.lastObject;
                
                point.subtitle = place.name;
            }];
            
            //需要将地图的显示区域变小
            MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 800, 800);
            [self.mapView setRegion:region animated:YES];
        });
    }];

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值