How can i make custom pin and use FollowWithHeading mode together?

297 views Asked by At

The following code below is show custom pin (picture as pin). it can use normally.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

   PVAttractionAnnotationView *annotationView = [[PVAttractionAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Attraction"];
   annotationView.canShowCallout = YES;
   return annotationView;

}

Then use following code to show current location

[self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading];

XCODE jump to main.m and show

Thread 1:Signal SIGABRT

On the other hand if i use the following code

[self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading];

and unused all of the following code

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

   PVAttractionAnnotationView *annotationView = [[PVAttractionAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Attraction"];
   annotationView.canShowCallout = YES;
   return annotationView;

}

Application will show current location normally but it's not show custom pin. It's show the red pin that is the default of system cause i've unused that code.

How can i make custom pin and use FollowWithHeading mode together?

..I'm sorry I do not use English well.

1

There are 1 answers

2
Paulw11 On BEST ANSWER

You need a slight change to your viewForAnnotation that examines the class of the annotation and returns the appropriate view. By returning nil the system will use the default view. You also need some additional code to implement view re-use correctly -

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *annotationView=nil;
    if ([annotation isKindOfClass:[PVAttractionAnnotation class]])  // Note - put your custom annotation class here
    {
        annotationView =(MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"Attraction"];
        if (annotationView == nil)
        {
            annotationView = [[PVAttractionAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Attraction"];  
            annotationView.canShowCallout = YES; 
        }
        else
        {
            annotationView.annotation=annotation;
        }

    }
    return annotationView;
}