標籤:
引入兩個架構,編碼寫成屬性,初始化後,傳入一個經緯度之後,展示地圖:
#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 *[email protected]{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard)};
MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:mkplacemark];
[mapItem openInMapsWithLaunchOptions:options];
}];
}
iOS22 地圖定位- 蘋果內建地圖