您好,登錄后才能下訂單哦!
//第一步:創(chuàng)建標注Annotation類,實現(xiàn)MKAnnotation協(xié)議
#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;
//設(shè)置地圖類型
mapView.mapType = MKMapTypeStandard;
//設(shè)置當(dāng)前用戶的位置
mapView.showsUserLocation = YES;
//設(shè)置經(jīng)緯度
CLLocationCoordinate2D center = {39.9145402256,116.1767592387};
//設(shè)置精確度,數(shù)值越大,顯示的范圍越大
MKCoordinateSpan span = {0.1,0.1};
//創(chuàng)建一個區(qū)域
MKCoordinateRegion region = {center,span};
//設(shè)置顯示的區(qū)域
[mapView setRegion:region animated:YES];
[self.view addSubview:mapView];
//第二步:創(chuàng)建標注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 = @"石景山區(qū)蘋果園";
CLLocationCoordinate2D coord3 = {39.8637359235,116.2854074940};
AnnotationModel *model3 = [[AnnotationModel alloc] initWithCoordinate:coord3];
model3.title = @"豐臺公園";
model3.subtitle = @"豐臺區(qū)豐臺公園";
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);
}
//第四步:最后實現(xiàn)MKMapViewDelegate協(xié)議方法,在該代理中創(chuàng)建MKPinAnnotationView標注視圖
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
NSLog(@"annotation:%@",annotation);
//自己的位置也是一個標注視圖,它是一個屬于MKUserLocation這么一個類
if ([annotation isKindOfClass:[MKUserLocation class]]) {
//如果滿足條件,說明當(dāng)前用戶自己的位置,不需要我們設(shè)置標注視圖
return nil;
}
static NSString *identifier = @"annotationView";
//MKPinAnnotationView是一個大頭針視圖
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
//設(shè)置大頭針的顏色
annotationView.pinColor = MKPinAnnotationColorPurple;
//設(shè)置顯示從天而降的動畫
annotationView.animatesDrop = YES;
//是否顯示標題視圖
annotationView.canShowCallout = YES;
//設(shè)置輔助視圖
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
//防止復(fù)用
annotationView.annotation = annotation;
return annotationView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。