地图与位置服务笔记
//第一步:創建標注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
總結
- 上一篇: linux学习之shell基础篇
- 下一篇: docker 核心概念整理