MKAnnotationView和MKPinAnnotationView的区别 代码示例


如果需要自定义大头针图片则创建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;

    }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值