iOS22 地图定位- 苹果自带地图

引入两个框架,编码写成属性,初始化后,传入一个经纬度之后,展示地图:

#import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> @interface YTMapappleController () @property (nonatomic,strong) CLGeocoder *geocoder; @end @implementation YTMapappleController - (void)viewDidLoad { [super viewDidLoad]; _geocoder=[[CLGeocoder alloc]init]; [self location]; // Do any additional setup after loading the view. } -(void)location{ [_geocoder reverseGeocodeLocation:[[CLLocation alloc]initWithLatitude:39.0 longitude:116.0] completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { CLPlacemark *clPlacemark=[placemarks firstObject]; //定位地标转化为地图的地标 MKPlacemark *mkplacemark=[[MKPlacemark alloc]initWithPlacemark:clPlacemark]; NSDictionary *options=@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard)}; MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:mkplacemark]; [mapItem openInMapsWithLaunchOptions:options]; }]; }
// 根据一个地址的名字 进行展示地图:
-(void)location{

  [_geocoder geocodeAddressString:@"北京市" completionHandler:^(NSArray *placemarks, NSError *error) {
        //获取第一个地标
        CLPlacemark *clPlacemark=[placemarks firstObject];
        //定位地标转化为地图的地标
        MKPlacemark *mkplacemark=[[MKPlacemark alloc]initWithPlacemark:clPlacemark];
        NSDictionary *options=@{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard)};
        MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:mkplacemark];
        [mapItem openInMapsWithLaunchOptions:options];
    }];

}

 

转载于:https://www.cnblogs.com/ytmaylover/p/5058213.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值