自定义 MKA 注释类



>我正在尝试访问在我的MapAnnotation自定义注释类中设置的"imageurl"属性。但是,当尝试在注释视图方法中访问它时,我收到错误

选择器"imageurl"没有已知的实例方法

如果我尝试标题,描述或副标题,它们工作正常。

如何访问注释的图像网址属性。

地图注释.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];
}

问题已解决

将 [视图注释] 转换为 (地图注释 *)

- (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];
}

最新更新