Question

I'm trying to access the "imageurl" property which is set in my MapAnnotation custom annotation class. However when trying to access it in the annotationView method I'm getting an error

No known instance method for selector 'imageurl'

If i try title, description or subtitle they work fine.

How can I access the imageurl property for an annotation.

MapAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapAnnotation : NSObject <MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *description;
NSString *imageurl;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *description;
@property (nonatomic, copy) NSString *imageurl;
@end

MapViewController.m

#import "MapViewController.h"
#import "AppDelegate.h"
#import "MapDetailViewController.h"
#import "MapAnnotation.h"

@interface MapViewController ()
@end

MapViewController.m    // Set span to cover area
MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);

// Set region
MKCoordinateRegion regionToDisplay = MKCoordinateRegionMake(center, span);
[self.nearbyMapView setRegion: regionToDisplay];

for (int i = 0; i < [[appDelegate offersFeeds] count]; i++)
{

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    NSString *plotAddress = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:@"addressline"];
    NSString *plotTitle = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:@"title"];
    NSString *plotSubTitle = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:@"subtitle"];
    NSString *plotDescription = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:@"description"];
    NSString *plotImageurl = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:@"imageurl"];

    [geocoder geocodeAddressString:plotAddress completionHandler:^(NSArray *placemarks, NSError *error) {
        if (placemarks && placemarks.count > 0)
        {
            CLPlacemark *topResult = [placemarks objectAtIndex:0];
            MKPlacemark *placemark = [[MKPlacemark alloc]initWithPlacemark:topResult];


            MapAnnotation *annotation = [[MapAnnotation alloc] init];
            annotation.coordinate = placemark.location.coordinate;
            annotation.title = plotTitle;
            annotation.subtitle = plotSubTitle;
            annotation.description = plotDescription;
            annotation.imageurl = plotImageurl;
            [self.nearbyMapView addAnnotation:annotation];
        }
    }];
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MapAnnotation class]])
{
    MKPinAnnotationView *pinView = nil;

    static NSString *defaultPinID = @"identifier";
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if (pinView == nil)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        pinView.pinColor = MKPinAnnotationColorRed;  //or Green or Purple

        pinView.enabled = YES;
        pinView.animatesDrop = YES;
        pinView.canShowCallout = YES;

        //Accessoryview for the annotation view in ios.
        pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        //Add cobalt logo to the leftCallout
        pinView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"PinImage"]];

    }
    else
    {
        pinView.annotation = annotation;
    }

    return pinView;
}
return nil;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{

description = [[view annotation] description];
myimage = [[view annotation] imageurl]; //NOT WORKING

[self performSegueWithIdentifier:@"showDetail" sender:self];
}
Was it helpful?

Solution

Problem Solved,

Cast the [view annotation] to (MapAnnotation *)

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
  MapAnnotation *annotation = = (MapAnnotation *)[view annotation];
  description = [annotation description];
  myimage = [annotation imageurl];

  [self performSegueWithIdentifier:@"showDetail" sender:self];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top