Custom Pins in IOS maps

Source: Internet
Author: User

1. Inherit the Mkannotationview.

HYAnnotationView.h file:

+ (Instancetype) Annotationviewwithmapview: (Mkmapview *) Mapview;

HYANNOTATIONVIEW.M file:

#import "HYAnnotation.h"//header file for import pin model

@interface Hyannotationview ()

@property (nonatomic, weak) Uiimageview *iconview;

@end

@implementation Hyannotationview

+ (Instancetype) Annotationviewwithmapview: (Mkmapview *) Mapview

{

static NSString *id = @ "Anno";

Hyannotationview *annotationview = (Hyannotationview *) [Mapview Dequeuereusableannotationviewwithidentifier:id];

if (Annotationview = = nil) {

Incoming loops use identities to create a pin control

Annotationview = [[Hmannotationview alloc] Initwithannotation:nil Reuseidentifier:id];

}

return annotationview;

}

-(ID) initwithannotation: (id<mkannotation>) annotation reuseidentifier: (NSString *) reuseidentifier

{

if (self = [super Initwithannotation:annotation Reuseidentifier:reuseidentifier]) {

Show titles and subheadings when you customize a PIN, you must write it if you want to display the title and sub-headings

Self.canshowcallout = YES;

Start customizing your PIN here, and you can create a view

[Self setsubview];

}

return self;

}

Custom Pins

-(void) setsubview{

Self.bounds = CGRectMake (0.f, 0.F, Kwidth, kheight);

UILabel *namelab =[[uilabel Alloc]init];

Namelab.width = 40;

Namelab.height = 30;

NAMELAB.Y = 0;

namelab.x = (self.width-namelab.width) * 0.5;

Namelab.font =iwtextfont16;

Namelab.backgroundcolor =iwtextcolorred;

Namelab.textcolor =[uicolor Whitecolor];

NameLab.layer.cornerRadius = 8.0;

NameLab.layer.borderWidth = 1.0;

NameLab.layer.borderColor =[uicolor Clearcolor]. Cgcolor;

Namelab.clipstobounds = true;//Remove boundary

Namelab.textalignment =nstextalignmentcenter;

[Self Addsubview:namelab];

Self.namelab =namelab;

}

Set properties on a PIN

-(void) Setannotation: (hmannotation *) annotation

{

[Super Setannotation:annotation];

}

When you click on a pin, add a custom view to the PIN (if you want to put TableView you can put TableView in the view)

-(void) setselected: (BOOL) selected animated: (BOOL) animated

{

if (self.selected = = selected) return;

if (selected)

{

if (Self.calloutview = nil)//self.calloutview is the property of the custom view

{

Self.calloutview = [[Zlcustomcalloutview alloc] Initwithframe:cgrectmake (0, 0, Kcalloutwidth, kcalloutheight)];

Self.calloutView.centerX =self.width/2.f+ + self.calloutoffset.x;

Self.calloutView.center = Cgpointmake (Cgrectgetwidth (self.bounds)/2.f + self.calloutoffset.x,

-cgrectgetheight (self.calloutView.bounds)/2.f + Self.calloutoffset.y) ;

}

[Self addSubview:self.calloutView];

}

Else

{

[Self.calloutview Removefromsuperview];

}

[Super setselected:selected animated:animated];

}

-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *) event

{

BOOL inside = [Super Pointinside:point withevent:event];

if (!inside && self.selected)

{

Inside = [Self.calloutview pointinside:[self convertpoint:point ToView:self.calloutView] withevent:event];

}

return inside;

}

At the time of use:

#pragma mark-mkmapviewdelegate

-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (hyannotation *) annotation

{

Returning nil will follow the system's default practice

if (![ Annotation Iskindofclass:[hyannotation class]) return nil;

1. Get the Pin control

Hyannotationview *annoview = [Hyannotationview Annotationviewwithmapview:mapview];

2. Transfer model

annoview.annotation = annotation;

return annoview;

}

Custom Pins in IOS maps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.