@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;
}
原文地址:http://10577688.blog.51cto.com/10567688/1697915