苹果自带地图

这篇博客介绍了如何在iOS应用中直接使用苹果内置的MapKit框架,无需依赖第三方库。通过导入<MapKit/MapKit.h>和<CoreLocation/CoreLocation.h>头文件,实现MKMapViewDelegate和CLLocationManagerDelegate代理方法,设置地图显示类型、缩放、滑动和旋转功能,并显示用户当前位置。通过定位到特定经纬度,展示了如何在地图上显示指定位置。

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

使用苹果地图 不需要使用第三方 不需要导入依赖库

直接在VC.m中
导入头文件
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
给地图的代理方法
<MKMapViewDelegate,CLLocationManagerDelegate>
定义属性
@property(nonatomic,strong)MKMapView *mapview;
下面就是代码了

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //初始化
    self.mapview = [[MKMapView alloc] initWithFrame:self.view.frame];
    //设置地图的显示类型
    [self.mapview setMapType:MKMapTypeMutedStandard];
    //设置缩放
    [self.mapview setZoomEnabled:YES];
    //设置滑动
    [self.mapview setScrollEnabled:YES];
    //设置旋转
    [self.mapview setRotateEnabled:YES];
    //显示当前的位置
    [self.mapview setShowsUserLocation:YES];
    //代理方法
    self.mapview.delegate = self;
    //当前八维的位置
    [self locateToLatitude:40.045612 longtitude:116.30612];

    [self.view addSubview:self.mapview];

}
-(void)locateToLatitude:(CLLocationDegrees)latitude longtitude:(CLLocationDegrees)longtitude{
//设置地图中心的经纬度
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(latitude, longtitude);

//设置地图显示范围
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);

//显示
[self.mapview setRegion:region animated:YES];

}

显示的地图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值