• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
hellowbabybaby
博客园    首页    新随笔    联系   管理    订阅  订阅

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];
    }];

}

 

posted @ 2015-12-18 21:02  hellowbabybaby  阅读(309)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3