James Wolfe
James Wolfe

Reputation: 360

How to change the look of my MKMapViewAnnotation

I have set the region of my map and added a pin to it, however I was wondering if it was possible to change the look of the pin?

I wanted to make it look like the icon that appears when you set ShowUsersLocation to YES.

Here is my code?

MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:mycoordinate];
region.center.latitude = mycoordinate.latitude;
region.center.longitude = mycoordinate.longitude;
region.span.longitudeDelta = 0.005f;
region.span.longitudeDelta = 0.005f;
[mapView addAnnotation:annotation];
[mapView setRegion:region animated:NO];

Upvotes: 0

Views: 36

Answers (1)

audience
audience

Reputation: 2412

Implement the - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation delegate method. See the Documentation.

  1. Set the delegate of the map view to the class you setup your pins (e.g. your Controller).
  2. Implement the - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation delegate method.

Basic implementation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    // Leave the user location pin as it is
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }
    static NSString *annotationReuseIdentifier = @"Annotation";
    MKAnnotationView *annotationView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:annotationReuseIdentifier];
    if (annotationView == nil) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationReuseIdentifier];
    }
    annotationView.annotation = annotation;

    // Set up your annotation view here

    return annotationView;
}

If you want more control of how the annotation looks like, you can implement your own annotation view (as a subclass of MKAnnotationView).

To show a pin like the user location dot, make an image of the dot and add it to the annotation view or draw the dot with the QuartzCore Framework.

Upvotes: 1

Related Questions