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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

ios地图小例子和手势的使用 供大家参考一下呦

發布時間:2024/4/17 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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地图小例子和手势的使用 供大家参考一下呦的全部內容,希望文章能夠幫你解決所遇到的問題。

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