iOS 地图

本文详细介绍如何在iOS应用中集成并定制MapKit地图组件,包括地图样式设置、用户位置显示及地理编码等功能。

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

/**

 使用地图需要导入 MapKit

 同样也需要 请求用户授权

 CoreLocation 是数据类 定位信息 地理编码 反地理编码

 MapKit控件 显示在屏幕上的视图

 MK开头

 地图:MKMapView

 大头针视图:MKPinAnnotationView


确定位置(在Info.plist里面添加以下代码)

NSLocationWhenInUseUsageDescription  (当使用软件时)
 NSLocationAlwaysUsageDescription (一直允许软件使用定位服务)

 */


- (void)viewDidLoad {

    [super viewDidLoad];

    

    if (![CLLocationManager  locationServicesEnabled]) {

        NSLog(@"没打开");

        return;

    }

    //不管是定位 还是使用地图  都需要授权

    manager = [[CLLocationManager alloc]init];

    [manager requestWhenInUseAuthorization];

    //不管是定位 还是使用地图  都需要授权

    

    //初始化地图,添加地图View

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

    //设置地图的样式

    /*

     MKMapTypeStandard = 0, 默认的普通样式

     MKMapTypeSatellite, 卫星

     MKMapTypeHybrid  鸟瞰

     */

    myMapView.mapType =MKMapTypeStandard;

    //设置是否允许用户旋转地图

    myMapView.rotateEnabled = YES;

    //设置允许放大缩小地图

    myMapView.zoomEnabled = YES;

    //设置是否允许地图滚动

    myMapView.scrollEnabled = YES;

    //设置是否有3D效果

    myMapView.pitchEnabled = NO;

    //设置是否显示用户的位置

    myMapView.showsUserLocation = YES;

    //设置用户移动地图的样式

//    myMapView.userTrackingMode = MKUserTrackingModeFollow;

    

    //设置是否显示建筑物

    //    Affects MKMapTypeStandard 必须在 MKMapTypeStandard有效

    myMapView.showsBuildings = NO;

    //是否允许显示兴趣点

    // Affects MKMapTypeStandard and MKMapTypeHybrid 必须在 普通样式 鸟瞰 样式 有效

    myMapView.showsPointsOfInterest = NO;

    myMapView.delegate = self;

    [self.view addSubview:myMapView];

    

    

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

    [myMapView addGestureRecognizer:addPoint];

}


#pragma mark  长按手势

- (void)addPointView:(UILongPressGestureRecognizer *)sender

{

    //解决长按手势  触发两次

    //在手势开始的时候  实行

    if (sender.state == UIGestureRecognizerStateBegan) {

    //把视图上面点 (point) 转换成  地图上面的坐标

        CGPoint point = [sender locationInView:myMapView];

        NSLog(@"%f %f",point.x,point.y);

        

        CLLocationCoordinate2D coordinate = [myMapView convertPoint:point toCoordinateFromView:myMapView];

//        NSLog(@"")

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

        pointAnnotation.coordinate = coordinate;

        [myMapView addAnnotation:pointAnnotation];

    }

}



#pragma mark  ------地图的代理方法

- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView

{

    NSLog(@"开始加载");

}


- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView

{

    NSLog(@"完成加载");

}


- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error

{

    NSLog(@"加载错误:%@",error);

}


//将要开始定位  用户位置的时候 调用

- (void)mapViewWillStartLocatingUser:(MKMapView *)mapView

{

    NSLog(@"将要开始定位  用户位置的时候");

}


//已经停止定位 用户位置的时候 调用

- (void)mapViewDidStopLocatingUser:(MKMapView *)mapView

{

    NSLog(@"已经停止定位  用户位置的时候");

}


//已经更新完毕 用户位置的时候 调用

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

    NSLog(@"已经更新完毕 用户位置的时候");

    

    //设置地图的区域范围

    

    /*

     //     经纬度 变化的比例范围

     typedef struct {

     CLLocationDegrees latitudeDelta;

     CLLocationDegrees longitudeDelta;

     } MKCoordinateSpan;

     

     //     区域范围

     //     center 为中心span为比例

     typedef struct {

     CLLocationCoordinate2D center;

     MKCoordinateSpan span;

     } MKCoordinateRegion;

     */

    

    MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);

    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);

    //如果使用Region的时候 用户移动  出现不断跳动的效果  可以屏蔽动画

    //myMapView.region = region;

    [myMapView setRegion:region animated:YES];

    

    

    //把地理位置 转换成名字

    //大头针

    //1、数据  MKPointAnnotation    继承自 <MKAnnotation>

    //2、视图  MKPinAnnotationView  继承自  MKAnnotationView

    //大头针   可以理解为  表示图的cell

    

    //使用大头针(标记)数据  可以使用系统提供的MKPointAnnotation  也可以自定义MKAnnotation

    

    //地图添加大头针数据 addAnnotation

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

    //setCoordinate 设置大头针的经纬度

    [annotation setCoordinate:userLocation.location.coordinate];

    

    

    

    //地理编码   反地理编码 比较耗时 (并且使用了异步的方式)所以 在添加大头针标题副标题的时候 需等 转换完毕再添加

    //把定位到的用户位置  转换成地名

    CLGeocoder *geocoder = [[CLGeocoder alloc]init];

    //反地理编码

    [geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {

    //placemarks  数组里面 存了 城市地标

        CLPlacemark *placemark = [placemarks lastObject];

        NSLog(@"城市名:%@ 街道名:%@",placemark.locality,placemark.thoroughfare);

    //给大头针 添加标题

        annotation.title = placemark.locality;

    //大头针副标题

        annotation.subtitle = placemark.name;

    //把大头针数据 添加到地图

        [myMapView addAnnotation:annotation];

        

    }];

    NSLog(@"...");

}


//定位失败

- (void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error

{

    NSLog(@"定位失败");

}


/*

     坐标转点

     - (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView *)view;

     点转坐标

     - (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view;

     区域范围转 CGRect

     - (CGRect)convertRegion:(MKCoordinateRegion)region toRectToView:(UIView *)view;

     CGRect  区域范围

     - (MKCoordinateRegion)convertRect:(CGRect)rect toRegionFromView:(UIView *)view;

     */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值