您的位置:首页 > 移动开发 > IOS开发

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

2015-12-18 21:02 330 查看
引入两个框架,编码写成属性,初始化后,传入一个经纬度之后,展示地图:

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: