地图设置分为三个步骤:
- 引入
#import <MapKit/MapKit.h>
,遵守代理MKMapViewDelegate
,创建地图视图MKMapView
- 设置经纬度
CLLocationCoordinate2D
- 设置跨度
MKCoordinateSpan
- 设置地图显示区域
MKCoordinateRegion
- 添加大头针
MKPointAnnotation
代码如下:
_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(18, 10, WIDTH-36, 230)];
_mapView.mapType = MKMapTypeStandard;
_mapView.delegate = self;
[self.view addSubview:_mapView];//这里是个坑,要先添加视图才能进行下面的步骤
//经纬度
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(_latitude, _longitude);
//跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
//地图显示区域
MKCoordinateRegion region = MKCoordinateRegionMake(coordinate,span);
[_mapView setRegion:region animated:YES];
//大头针
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
[_mapView addAnnotation:annotation];
实现代理方法
#pragma mark - 地图代理
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"annotationView"];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotationView"];
}
annotationView.image = [UIImage imageNamed:@"dingwei_ditu"];
return annotationView;
}
有关更高级自定义大头针和导航线的实现,移步:http://www.itnose.net/detail/6201227.html