- Create your own annotation class (
MKAnnotation ) - Create your own AnnotationView class (
MKAnnotationView )
Make sure that you define the title, background image, and other custom annotation elements in the AnnotationView class as properties.
Cancel the following method and set the values of the properties defined in the AnnotationView class.
- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ if ([<Object of CustomAnnotation class> isKindOfClass:[MKUserLocation class]]) return nil; CustomAnnotation* custom_anno = (CustomAnnotation*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"custom_annotation"]; if (!custom_anno){ custom_anno = [[CustomAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:@"custom_annotation"]; custom_anno.frame = CGRectMake(0, 0, 250, 57.5); custom_anno.canShowCallout = NO;
My custom annotation class was “Annotation,” and my custom annotation view class was “CustomAnnotation.” Change according to your needs.
After that, just create an object of the Annotation class and use it.
EDIT:
Make sure you override the follwing method in your class of custom annotation view:
in .h:
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
in .m:
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; if (self) { } return self;
}
EDIT 2:
You can use it in your view controller as follows:
MKMapView* cellMap = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 290, 100)]; cellMap.delegate = self; cellMap.userInteractionEnabled = NO; CLLocationCoordinate2D center = CLLocationCoordinate2DMake(<your coordinates>); MKCoordinateSpan span = MKCoordinateSpanMake(0.04, 0.04); MKCoordinateRegion region = MKCoordinateRegionMake(center, span); cellMap.showsUserLocation = NO; [cellMap setRegion:region animated:YES]; Annotation* anon = [[Annotation alloc] init]; anon.coordinate = center; [cellMap addAnnotation:anon]; [checkIn addSubview:cellMap];
source share