//第一步:创建标注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
{
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
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。