如果需要自定义大头针图片则创建MKAnnotationView
如果要使用苹果自带的大头针则创建MKPinAnnotationView
代码:
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController () <MKMapViewDelegate>
{
MKMapView *_mapView;
}
@end
@implementation ViewController
- (void)dealloc
{
[_mapViewrelease];
[superdealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
_mapView = [[MKMapViewalloc]initWithFrame:CGRectMake(0,0,320,480)];
_mapView.delegate =self;
_mapView.mapType =MKMapTypeStandard;
_mapView.showsUserLocation =YES;
_mapView.userLocation.title =@"Me";
_mapView.userLocation.subtitle =@"tips";
_mapView.userTrackingMode =MKUserTrackingModeFollow;
CLLocationCoordinate2D coordinate = {34.722148,113.74119};
[_mapViewsetCenterCoordinate:coordinateanimated:YES];
[self.viewaddSubview:_mapView];
MKPointAnnotation *annotation = [[MKPointAnnotationalloc]init];
annotation.coordinate = coordinate;
annotation.title =@"自定标注";
annotation.subtitle =@"is here";
[_mapViewaddAnnotation:annotation];
[annotationrelease];
}
#pragma mark - MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if (![annotationisKindOfClass:[MKUserLocationclass]])
{
staticNSString *annotationIdentifier =@"annotation";
MKAnnotationView *annotationView = [mapViewdequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!annotationView)
{
annotationView = [[[MKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:annotationIdentifier]autorelease];
}
//标注的图片
annotationView.image = [UIImageimageNamed:@"arrest"];
//是否可以弹出详情
annotationView.canShowCallout =YES;
//左侧详情view
UIImageView *imageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"imgTask6"]];
imageView.frame =CGRectMake(0,0,40, 40);
annotationView.leftCalloutAccessoryView = imageView;
[imageViewrelease];
//右侧详情view
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = btn;
return annotationView;
}
else
{
staticNSString *annotationIdentifier =@"userLocation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapViewdequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!annotationView)
{
annotationView = [[[MKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:annotationIdentifier]autorelease];
}
annotationView.canShowCallout =YES;
//标注视图降落动画
annotationView.animatesDrop =YES;
//颜色
annotationView.pinColor =MKPinAnnotationColorGreen;
return annotationView;
}
}