@interface ViewController ()
{
CLLocationManager *manager;
}
@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.9115696831,116.2396262413};
//設置精確度,數值越大,顯示的範圍越大
MKCoordinateSpan span = {0.1, 0.1};
//創建一個區域
MKCoordinateRegion region = {center, span};
//設置顯示的區域
[mapView setRegion:region animated:YES];
[self.view addSubview:mapView];
//第二部 創建標註Annotation對象
CLLocationCoordinate2D cl1 = {39.9231997850,116.1998431153};
AnotationModel *model1 = [[AnotationModel alloc] initWithCoordinate:cl1];
model1.title = @"八寶山";
model1.subtitle = @"hehe八寶山";
CLLocationCoordinate2D cl2 = {39.9190639028,116.1901162217};
AnotationModel *model2 = [[AnotationModel alloc] initWithCoordinate:cl2];
model2.title = @"石景山體育場";
model2.subtitle = @"就是體育場";
CLLocationCoordinate2D cl3 = {39.9013418421,116.5168125821};
AnotationModel *model3 = [[AnotationModel alloc] initWithCoordinate:cl3];
model3.title = @"北京東站";
model3.subtitle = @"hehe北京東站";
CLLocationCoordinate2D cl4 = {39.8566488252,116.3856497732};
AnotationModel *model4 = [[AnotationModel alloc] initWithCoordinate:cl4];
model4.title = @"bj南站";
model4.subtitle = @"就是南站";
//第三步 將Annotation對象添加到MapView中
[mapView addAnnotation:model1];
[mapView addAnnotation:model2];
[mapView addAnnotation:model3];
[mapView addAnnotation:model4];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -MKMapViewDelegate
#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(@"anotation: %@", 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;
}