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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

地图与位置服务笔记

發布時間:2025/6/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 地图与位置服务笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

//第一步:創建標注Annotation類,實現MKAnnotation協議

#import "ViewController.h"

#import "AnnotationModel.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view, typically from a nib.

? ? manager = [[CLLocationManager alloc] init];

?? ?

? ? [manager requestAlwaysAuthorization];

?? ?

? ? MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];

? ? mapView.delegate = self;

? ? //設置地圖類型

? ? mapView.mapType = MKMapTypeStandard;

? ? //設置當前用戶的位置

? ? mapView.showsUserLocation = YES;

?? ?

? ? //設置經緯度

? ? CLLocationCoordinate2D center = {39.9145402256,116.1767592387};

?? ?

? ? //設置精確度,數值越大,顯示的范圍越大

? ? MKCoordinateSpan span = {0.1,0.1};

?? ?

? ? //創建一個區域

? ? MKCoordinateRegion region = {center,span};

?? ?

? ? //設置顯示的區域

? ? [mapView setRegion:region animated:YES];

?? ?

? ? [self.view addSubview:mapView];

?? ?

? ? //第二步:創建標注Annotation對象

? ? CLLocationCoordinate2D coord1 = {39.9117669028,116.1933922217};

? ? AnnotationModel *model1 = [[AnnotationModel alloc] initWithCoordinate:coord1];

? ? model1.title = @"石景山公園";

? ? model1.subtitle = @"北京石景山公園";

?? ?

? ? CLLocationCoordinate2D coord2 = {39.9261543081,116.1776545774};

? ? AnnotationModel *model2 = [[AnnotationModel alloc] initWithCoordinate:coord2];

? ? model2.title = @"蘋果園";

? ? model2.subtitle = @"石景山區蘋果園";

?? ?

? ? CLLocationCoordinate2D coord3 = {39.8637359235,116.2854074940};

? ? AnnotationModel *model3 = [[AnnotationModel alloc] initWithCoordinate:coord3];

? ? model3.title = @"豐臺公園";

? ? model3.subtitle = @"豐臺區豐臺公園";

?? ?

? ? CLLocationCoordinate2D coord4 = {39.9128821069,116.3971393161};

? ? AnnotationModel *model4 = [[AnnotationModel alloc] initWithCoordinate:coord4];

? ? model4.title = @"故宮博物館";

? ? model4.subtitle = @"北京市故宮博物館";

?? ?

? ? //第三步:將Annotation對象添加到MapView

? ? [mapView addAnnotation:model1];

? ? [mapView addAnnotation:model2];

? ? [mapView addAnnotation:model3];

? ? [mapView addAnnotation:model4];


?? ?

}

#pragma mark -MKMapViewDelegate

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

? ? NSLog(@"userLocation:%@",userLocation);

}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view

{

? ? NSLog(@"view is %@",view);

}

//第四步:最后實現MKMapViewDelegate協議方法,在該代理中創建MKPinAnnotationView標注視圖

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

{

? ? NSLog(@"annotation:%@",annotation);

?? ?

? ? //自己的位置也是一個標注視圖,它是一個屬于MKUserLocation這么一個類

? ? if ([annotation isKindOfClass:[MKUserLocation class]]) {

? ? ? ? //如果滿足條件,說明當前用戶自己的位置,不需要我們設置標注視圖

? ? ? ? return nil;

? ? }

? ? static NSString *identifier = @"annotationView";

? ? //MKPinAnnotationView是一個大頭針視圖

? ? MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

? ? if (annotationView == nil) {

? ? ? ? annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];

? ? ? ? //設置大頭針的顏色

? ? ? ? annotationView.pinColor = MKPinAnnotationColorPurple;

? ? ? ? //設置顯示從天而降的動畫

? ? ? ? annotationView.animatesDrop = YES;

? ? ? ? //是否顯示標題視圖

? ? ? ? annotationView.canShowCallout = YES;

? ? ? ? //設置輔助視圖

? ? ? ? annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

? ? }

? ? //防止復用

? ? annotationView.annotation = annotation;

? ? return annotationView;

}

- (void)didReceiveMemoryWarning {

? ? [super didReceiveMemoryWarning];

? ? // Dispose of any resources that can be recreated.

}


@end


轉載于:https://blog.51cto.com/10585085/1697680

總結

以上是生活随笔為你收集整理的地图与位置服务笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

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