ios地图小例子和手势的使用 供大家参考一下呦
生活随笔
收集整理的這篇文章主要介紹了
ios地图小例子和手势的使用 供大家参考一下呦
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近做了一個小例子 做點筆記 供剛入職場的菜鳥學習,也供自己記憶。
目標:在地圖上加上標記 ?同時復習一下手勢的使用
效果圖:
?
具體代碼
導入框架:MapKit.framework
創建一個新類 繼承NSObject ?叫做MyAnnotation ? 并在后邊加上<MKAnnotation>
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MyAnnotation : NSObject<MKAnnotation> @property (nonatomic, assign) CLLocationCoordinate2D coordinate; @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *subtitle; @end?viemController.m的代碼
#import "ViewController.h" #import <MapKit/MapKit.h> #import "MyAnnotation.h" @interface ViewController ()<MKMapViewDelegate> @property(nonatomic,strong)MKMapView *mapView;//添加一個MApView @property(nonatomic,strong)MKAnnotationView *annotationView;//添加一個標記view @end?
//對mapView 的響應的設置并將它加到跟視圖中
//添加一個標記
//創建手勢實現長按能夠加一個標記
- (void)viewDidLoad {[super viewDidLoad];//對mapView 的響應的設置并將它加到跟視圖中_mapView = [[MKMapView alloc]initWithFrame:self.view.frame];_mapView.mapType = MKMapTypeStandard;_mapView.delegate = self;[self.view addSubview:_mapView];//對annotation 的設置_annotationView = [[MKAnnotationView alloc]initWithFrame:self.view.frame];//添加一個標記//設置標記MyAnnotation *annotation = [[MyAnnotation alloc]init];//設置經緯度annotation.title = @"中國";annotation.subtitle = @"河北";annotation.coordinate =CLLocationCoordinate2DMake(40, 110);[self.mapView addAnnotation:annotation];//設置顯示標識的內容[_mapView setCenterCoordinate:annotation.coordinate animated:YES];//創建手勢實現長按能夠加一個標記UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];[self.view addGestureRecognizer:longPress];}?
//手勢長按的實現
-(void)longPress:(UILongPressGestureRecognizer *)sender {//獲得點擊點的坐標CGPoint point = [sender locationInView:self.view];//將點擊的地圖上點轉換成經緯點CLLocationCoordinate2D Mycoordinate= [self.mapView convertPoint:point toCoordinateFromView:self.mapView];MyAnnotation *annotation = [[MyAnnotation alloc]init];annotation.coordinate = Mycoordinate;annotation.title = @"河北";annotation.subtitle = @"石家莊";[self.mapView addAnnotation:annotation];}?
#pragma mark-mapView的代理方法-顯示標識的方法
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {//創建一個標識的IDstatic NSString *annotationID = @"annotation";//先從用戶的緩存里找大頭針視圖MKPinAnnotationView *view = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID];//如果沒有大頭針視圖,自己進行創建if(!view){view = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];}//對大頭針視圖的屬性進行設置//設置標記為傳進來的標記 顯示氣泡 大頭針的顏色view.annotation = annotation;view.canShowCallout = YES;view.pinColor = MKPinAnnotationColorRed;//左邊的副視圖view.leftCalloutAccessoryView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"0.png"]];return view; }?
轉載于:https://www.cnblogs.com/lishanshan/p/4886349.html
總結
以上是生活随笔為你收集整理的ios地图小例子和手势的使用 供大家参考一下呦的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言中简单的for循环和浮点型变量
- 下一篇: 2015 10月21日 工作计划与执行