Thursday, September 3, 2015

MKMapView - Custom View for Annotation

First step is to have MKMapView delegate set and override the viewForAnnotation method 
Next is to create annotation 

thePlane = [[MKPointAnnotation alloc] init];
    thePlane.coordinate = startPoint;
    thePlane.title = @"Agent";
    [self.mapView addAnnotation:thePlane];


- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *SFAnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView *pinView =
    (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
    if (!pinView)
    {
        MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                         reuseIdentifier:SFAnnotationIdentifier];
        UIImage *flagImage = [UIImage imageNamed:@"car.png"];
        annotationView.centerOffset = CGPointMake(0, -16);
        // You may need to resize the image here.
        annotationView.image = flagImage;
        return annotationView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;

}

references:
https://gist.github.com/siqin/3175036

No comments:

Post a Comment