Пользовательские изображения аннотацииПросмотритесь к щелчкам при нажатии - PullRequest
2 голосов
/ 05 апреля 2010

Я отображаю пользовательские изображения на карте (вместо пинов по умолчанию), используя код ниже. Однако когда я нажимаю на элемент (и появляется выноска), изображение возвращается к красной булавке по умолчанию. Как сохранить свое собственное изображение, даже когда отображается выноска?

- (MKAnnotationView *) mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinAnnotation = nil;

    if (annotation != mapView.userLocation) 
    {
        static NSString *pinID = @"mapPin";
        pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
        if (pinAnnotation == nil)
            pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID] autorelease];

        // Set the image
        pinAnnotation.image = [UIImage imageNamed:@"TestIcon.png"];

        // Set ability to show callout
        pinAnnotation.canShowCallout = YES;

        // Set up the disclosure button on the right side
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinAnnotation.rightCalloutAccessoryView = infoButton;

        [pinID release];
    }

    return pinAnnotation;
    [pinAnnotation release];
}

Ответы [ 2 ]

5 голосов
/ 23 ноября 2010

Я обнаружил, что, сделав pinAnnotation для MKAnnotationView, а не MKPinAnnotationView, я получил желаемый результат. Изображение больше не превращается в булавку

    static NSString *pinID = @"mapPin";
    pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
    if (pinAnnotation == nil)
        pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID] autorelease];

    // Set the image
    pinAnnotation.image = [UIImage imageNamed:@"TestIcon.png"];
0 голосов
/ 20 мая 2010

Вам нужно использовать подпредставление, а не свойство изображения. Этот код успешно решает проблему для меня:

    UIImage * image = [UIImage imageNamed:@"blue_pin.png"];
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
    [annView addSubview:imageView];
...