获取内容资料
iOS开发

ios地图开发教程,ios百度地图开发教程

本文来自 YungFan,作者 suiling在iOS开发中,地图也是很多App都需要使用的功能。本文主要对iOS中的地图知识点进行介绍。需要说明的是地图看似很复杂,其实它仅仅是一个控件,就和UIButton、UITableView等一样。本文代码环境为:Xcode 10.2。

地图既然是控件,就可以在StoryBoard和代码中使用

地图上如果想要显示用户的位置,必须与定位配合,那么就需要创建定位管理器、设置权限等,可以参考iOS开发之定位,同时需要设置地图的属性(代码设置也可以)如下图

showUserLocation

1.拖拽一个地图到控制器View中

StoryBoard中添加地图控件

2.拖拽IBOutlet

3.声明CLLocationManager

实现功能:显示地图,并且显示用户所在的位置,点击用户的位置,显示一个气泡展示用户的位置信息

@interface ViewController //地图 很多属性都在SB中配置了@property (weak, nonatomic) IBOutlet MKMapView *map;@property (strong, nonatomic) CLLocationManager *manager;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        [self showUserInfo];    }// 如果想显示用户的位置 只需要下面三行代码-(void)showUser{        _manager = [[CLLocationManager alloc]init];        [_manager requestAlwaysAuthorization];        _map.userTrackingMode = MKUserTrackingModeFollowWithHeading;    }// 改变用户蓝点点击后的气泡信息-(void)showUserInfo{        _map.delegate = self;        [self showUser];    }//通过代理改变userLocation的标题实现更改信息- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{           CLLocation *location =  userLocation.location;        CLGeocoder *geocoder = [[CLGeocoder alloc]init];        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {                CLPlacemark *mark = placemarks.firstObject;                userLocation.title = mark.locality;                userLocation.subtitle = mark.thoroughfare;      }];}@end效果

实现功能:在之前功能的基础上实现地图的任意视角(“缩放级别”)

@interface ViewController @property(nonatomic, strong) CLLocationManager *manager;@property (weak, nonatomic) IBOutlet MKMapView *map;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];       _manager = [[CLLocationManager alloc]init];        [_manager requestAlwaysAuthorization];        _map.showsUserLocation = YES;        _map.delegate = self;}//如何通过定位到的位置 设置地图的“缩放级别”?//通过设置地图的MKCoordinateRegion达到-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{        CLLocation *location = userLocation.location;        //设置地图显示的“区域”        //跨度,通过这个精细控制显示的地图视角    MKCoordinateSpan span = MKCoordinateSpanMake(0.003, 0.003);        //区域    MKCoordinateRegion region = MKCoordinateRegionMake(location.coordinate, span);        //让地图显示设置的区域    [_map setRegion:region];    }@end效果

功能:点击屏幕,可以添加标注

重写地图的代理方法,返回标注的样式

@interface MyAnnotation : NSObject /** *  大头针的位置 */@property (nonatomic, assign) CLLocationCoordinate2D coordinate;/** *  主标题 */@property (nonatomic,  copy, nullable) NSString *title;/** *  副标题 */@property (nonatomic,  copy, nullable) NSString *subtitle;- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;@end控制器

@interface ViewController @property(nonatomic, strong) CLLocationManager *manager;@property (weak, nonatomic) IBOutlet MKMapView *map;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        _manager = [[CLLocationManager alloc]init];        [_manager requestAlwaysAuthorization];        _map.showsUserLocation = YES;        _map.delegate = self;}//点击地图的任一位置 都可以插入一个标注,标注的标题和副标题显示的是具体位置-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{            //点击屏幕产生的坐标如何与地图的经纬度进行转换?        //1.获取点击的坐标    CGPoint touchPoint = [touches.anyObject locationInView:self.map];        //2.将点击的坐标转换成经纬度    CLLocationCoordinate2D coordinate =  [self.map convertPoint:touchPoint toCoordinateFromView:self.map];        //3.添加标注    MyAnnotation *annotation = [[MyAnnotation alloc]init];        annotation.coordinate = coordinate;        [self.map addAnnotation:annotation];}-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注