日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发之地图

發布時間:2025/3/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发之地图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在iOS開發中,地圖也是很多App都需要使用的功能。本文主要對iOS中的地圖知識點進行介紹。需要說明的是地圖看似很復雜,其實它僅僅是一個控件,就和UIButton、UITableView等一樣。本文代碼環境為:Xcode 10.2。

一、理論知識

  • 地圖既然是控件,就可以在StoryBoard和代碼中使用

  • 地圖上如果想要顯示用戶的位置,必須與定位配合,那么就需要創建定位管理器、設置權限等,可以參考iOS開發之定位,同時需要設置地圖的屬性(代碼設置也可以)如下圖

二、準備工作

  • 拖拽一個地圖到控制器View中
  • 拖拽IBOutlet
  • 聲明CLLocationManager
  • 聲明權限
  • 設置gpx數據
  • 二、地圖基本使用

    • 實現功能:顯示地圖,并且顯示用戶所在的位置,點擊用戶的位置,顯示一個氣泡展示用戶的位置信息
    • 代碼
    @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的標題實現更改信息 - (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 復制代碼
    • 效果

    三、地圖縮放級別

    • 實現功能:在之前功能的基礎上實現地圖的任意視角(“縮放級別”)
    • 代碼
    @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;//設置地圖顯示的“區域”//跨度,通過這個精細控制顯示的地圖視角MKCoordinateSpan span = MKCoordinateSpanMake(0.003, 0.003);//區域MKCoordinateRegion region = MKCoordinateRegionMake(location.coordinate, span);//讓地圖顯示設置的區域[_map setRegion:region];}@end 復制代碼
    • 效果

    四、添加標注

    • 功能:點擊屏幕,可以添加標注
    • 說明:添加標注分三步
      • 創建標注模型
      • 重寫地圖的代理方法,返回標注的樣式
      • 將標注添加到地圖
    • 代碼
      • 標注模型
    @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{//點擊屏幕產生的坐標如何與地圖的經緯度進行轉換?//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<MKAnnotation>)annotation{//判斷是不是用戶的數據模型 讓用戶位置的標注不一樣if ([annotation isKindOfClass:[MKUserLocation class]]) {return nil;}//1.從重用池取MKMarkerAnnotationView *annotationView = (MKMarkerAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"abc"];//2.沒有的時候創建if(annotationView == nil) {annotationView = [[MKMarkerAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"abc"];}return annotationView; }-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{CLLocation *location = userLocation.location;//設置地圖顯示的“區域”//跨度MKCoordinateSpan span = MKCoordinateSpanMake(0.013, 0.013);//區域MKCoordinateRegion region = MKCoordinateRegionMake(location.coordinate, span);//讓地圖顯示設置的區域[_map setRegion:region];}@end 復制代碼
    • 效果

    五、添加自定義標注

    • 實現功能:在前面的基礎上,自定義標注的樣式
    • 代碼:只需要更改上面的代理方法即可
    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{//判斷是不是用戶的數據模型 讓用戶位置的標注不一樣if ([annotation isKindOfClass:[MKUserLocation class]]) {return nil;}//1.從重用池取MKAnnotationViewMKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"abc"];//2.沒有的時候創建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开发之地图的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。