引入两个框架,编码写成属性,初始化后,传入一个经纬度之后,展示地图:
#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];
}];
}