地圖設(shè)置分為三個步驟:
- 引入
#import <MapKit/MapKit.h>
,遵守代理MKMapViewDelegate
司倚,創(chuàng)建地圖視圖MKMapView
- 設(shè)置經(jīng)緯度
CLLocationCoordinate2D
- 設(shè)置跨度
MKCoordinateSpan
- 設(shè)置地圖顯示區(qū)域
MKCoordinateRegion
- 添加大頭針
MKPointAnnotation
代碼如下:
_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(18, 10, WIDTH-36, 230)];
_mapView.mapType = MKMapTypeStandard;
_mapView.delegate = self;
[self.view addSubview:_mapView];//這里是個坑颤诀,要先添加視圖才能進(jìn)行下面的步驟
//經(jīng)緯度
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(_latitude, _longitude);
//跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
//地圖顯示區(qū)域
MKCoordinateRegion region = MKCoordinateRegionMake(coordinate,span);
[_mapView setRegion:region animated:YES];
//大頭針
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
[_mapView addAnnotation:annotation];
實現(xiàn)代理方法
#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;
}
有關(guān)更高級自定義大頭針和導(dǎo)航線的實現(xiàn),移步:http://www.itnose.net/detail/6201227.html