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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

MapsDemo

發布時間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MapsDemo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 #import "ViewController.h" 2 //位置 3 #import <CoreLocation/CoreLocation.h> 4 //地圖 5 #import <MapKit/MapKit.h> 6 //標注視圖 7 #import "HLAnnotation.h" 8 @interface ViewController ()<MKMapViewDelegate> 9 //位置的管理者 10 @property (nonatomic,strong) CLLocationManager *manager ; 11 //地圖的對象 12 @property (nonatomic,strong) MKMapView *mapView; 13 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 21 //創建位置管理者 22 self.manager = [[CLLocationManager alloc] init]; 23 if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { 24 25 [self.manager requestAlwaysAuthorization]; 26 [self.manager requestWhenInUseAuthorization]; 27 } 28 //創建地圖的對象 29 self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 30 [self.view addSubview:self.mapView]; 31 //地圖的類型 32 self.mapView.mapType = MKMapTypeStandard ; 33 //顯示用戶的信息 34 self.mapView.showsUserLocation = YES ; 35 //設置地圖的代理 36 self.mapView.delegate = self ; 37 38 //添加一個手勢 39 [self.mapView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGerture:)]]; 40 41 } 42 43 #pragma maek --長按手勢的關聯方法 44 -(void)longPressGerture:(UILongPressGestureRecognizer *)longPress 45 { 46 //獲取點擊點 47 CGPoint point = [longPress locationInView:longPress.view]; 48 49 //將觸摸得到的點,轉化為2D坐標的點 50 CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView]; 51 52 //創建一個標注視圖對象 53 HLAnnotation *annotation = [[HLAnnotation alloc] init]; 54 55 //給標注視圖設置一個地圖上的位置 56 annotation.coordinate = coordinate ; 57 58 //設置標題 59 annotation.title = @"china"; 60 61 //設置子標題 62 annotation.subtitle = @"廣州"; 63 64 static NSInteger a = 1 ; 65 66 //設置tag值 67 annotation.tag = a ; 68 a ++ ; 69 70 //將標注視圖添加到地圖上 71 [self.mapView addAnnotation:annotation]; 72 73 } 74 #pragma mark --自定義標注視圖樣式的方法 75 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 76 { 77 //如果系統的用戶位置的大頭針 78 if ([annotation isKindOfClass:[MKUserLocation class]]) { 79 80 return nil; 81 } 82 83 //創建一個重用標示符 84 static NSString *identifier = @"annotation"; 85 86 //創建一個重用隊列 87 MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 88 if (pin == nil) { 89 90 pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 91 } 92 //設置大頭針的往下墜落的效果 93 pin.animatesDrop = YES ; 94 //允許打開氣泡的屬性 95 pin.canShowCallout = YES ; 96 97 //創建氣泡的左右按鈕 98 UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; 99 leftButton.frame = CGRectMake(0, 0, 30, 30); 100 [leftButton setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal]; 101 pin.leftCalloutAccessoryView = leftButton ; 102 leftButton.tag = 10086 ; 103 104 UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; 105 rightButton.frame = CGRectMake(0, 0, 30, 30); 106 [rightButton setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal]; 107 pin.rightCalloutAccessoryView = rightButton ; 108 rightButton.tag = 10010 ; 109 110 111 return pin ; 112 } 113 114 #pragma mark --點擊大頭針附件按鈕時執行的方法 115 -(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 116 { 117 if (control ==[self.view viewWithTag:10086]) { 118 119 NSLog(@"左邊"); 120 } 121 else 122 { 123 NSLog(@"右邊"); 124 } 125 } 126 #pragma mark --點擊標注視圖執行的方法 127 -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 128 { 129 NSLog(@"view = %@",view); 130 } 131 132 #pragma mark --地圖區域發生改變時執行的方法 133 -(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated 134 { 135 NSLog(@"地圖區域發生改變時執行的方法"); 136 } 137 138 #pragma mark --更新用戶位置的方法 139 -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 140 { 141 NSLog(@"%@",userLocation.location); 142 143 //設置標題 144 userLocation.title = @"廣州市"; 145 userLocation.subtitle = @"天河區"; 146 //設置地圖的比例尺 147 MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01); 148 //設置范圍 149 MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span); 150 self.mapView.region = region ; 151 }

自定義標注視圖:繼承NSObject

1 #import <Foundation/Foundation.h> 2 #import <MapKit/MapKit.h> 3 @interface HLAnnotation : NSObject<MKAnnotation> 4 5 //MKAnnotation協議里面必須實現的屬性 6 @property (nonatomic) CLLocationCoordinate2D coordinate; 7 8 //MKAnnotation協議里面的可選屬性 9 @property (nonatomic, copy) NSString *title; 10 @property (nonatomic, copy) NSString *subtitle; 11 12 //設置一個tag值標記區分每一個標注視圖(大頭針) 13 @property (nonatomic) NSInteger tag ; 14 15 @end

?

轉載于:https://www.cnblogs.com/yyxblogs/p/4872876.html

總結

以上是生活随笔為你收集整理的MapsDemo的全部內容,希望文章能夠幫你解決所遇到的問題。

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