生活随笔
收集整理的這篇文章主要介紹了
iOS开发之地图
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在iOS開發(fā)中,地圖也是很多App都需要使用的功能。本文主要對iOS中的地圖知識點進行介紹。需要說明的是地圖看似很復雜,其實它僅僅是一個控件,就和UIButton、UITableView等一樣。本文代碼環(huán)境為:Xcode 10.2。
一、理論知識
二、準備工作
拖拽一個地圖到控制器View中 拖拽IBOutlet聲明CLLocationManager聲明權限設置gpx數(shù)據(jù)二、地圖基本使用
- 實現(xiàn)功能:顯示地圖,并且顯示用戶所在的位置,點擊用戶的位置,顯示一個氣泡展示用戶的位置信息
- 代碼
@interface ViewController ()<MKMapViewDelegate>//地圖 很多屬性都在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的標題實現(xiàn)更改信息
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{CLLocation *location = userLocation.location;CLGeocoder *geocoder = [[CLGeocoder alloc]init];[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {CLPlacemark *mark = placemarks.firstObject;userLocation.title = mark.locality;userLocation.subtitle = mark.thoroughfare;}];
}
@end
復制代碼三、地圖縮放級別
- 實現(xiàn)功能:在之前功能的基礎上實現(xiàn)地圖的任意視角(“縮放級別”)
- 代碼
@interface ViewController ()<MKMapViewDelegate>@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;//設置地圖顯示的“區(qū)域”//跨度,通過這個精細控制顯示的地圖視角MKCoordinateSpan span = MKCoordinateSpanMake(0.003, 0.003);//區(qū)域MKCoordinateRegion region = MKCoordinateRegionMake(location.coordinate, span);//讓地圖顯示設置的區(qū)域[_map
setRegion:region];}@end
復制代碼四、添加標注
- 功能:點擊屏幕,可以添加標注
- 說明:添加標注分三步
- 創(chuàng)建標注模型
- 重寫地圖的代理方法,返回標注的樣式
- 將標注添加到地圖
- 代碼
@interface MyAnnotation : NSObject <MKAnnotation>
/*** 大頭針的位置*/
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
/*** 主標題*/
@property (nonatomic, copy, nullable) NSString *title;
/*** 副標題*/
@property (nonatomic, copy, nullable) NSString *subtitle;
- (void)
setCoordinate:(CLLocationCoordinate2D)newCoordinate;@end
復制代碼@interface ViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>@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<UITouch *> *)touches withEvent:(UIEvent *)event{//點擊屏幕產(chǎn)生的坐標如何與地圖的經(jīng)緯度進行轉換?//1.獲取點擊的坐標CGPoint touchPoint = [touches.anyObject locationInView:self.map];//2.將點擊的坐標轉換成經(jīng)緯度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<MKAnnotation>)annotation{//判斷是不是用戶的數(shù)據(jù)模型 讓用戶位置的標注不一樣
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;}//1.從重用池取MKMarkerAnnotationView *annotationView = (MKMarkerAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@
"abc"];//2.沒有的時候創(chuàng)建
if(annotationView == nil) {annotationView = [[MKMarkerAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@
"abc"];}
return annotationView;
}-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{CLLocation *location = userLocation.location;//設置地圖顯示的“區(qū)域”//跨度MKCoordinateSpan span = MKCoordinateSpanMake(0.013, 0.013);//區(qū)域MKCoordinateRegion region = MKCoordinateRegionMake(location.coordinate, span);//讓地圖顯示設置的區(qū)域[_map
setRegion:region];}@end
復制代碼五、添加自定義標注
- 實現(xiàn)功能:在前面的基礎上,自定義標注的樣式
- 代碼:只需要更改上面的代理方法即可
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{//判斷是不是用戶的數(shù)據(jù)模型 讓用戶位置的標注不一樣
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;}//1.從重用池取MKAnnotationViewMKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@
"abc"];//2.沒有的時候創(chuàng)建
if(annotationView == nil) {annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@
"abc"];}//設置標注的圖片int i = arc4random() % 11;NSString *imgName = [NSString stringWithFormat:@
"icon_map_cateid_%d", i];annotationView.image = [UIImage imageNamed:imgName];//左邊視圖annotationView.leftCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@
"left"]];//右邊視圖annotationView.rightCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@
"right"]];annotationView.canShowCallout = YES;
return annotationView;
}
復制代碼六、案例代碼
GitHub地址
轉載于:https://juejin.im/post/5cc64d5a6fb9a032204fdfe8
總結
以上是生活随笔為你收集整理的iOS开发之地图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。