iPhone地图视图自定义注释

时间:2011-11-08 12:59:47

标签: iphone objective-c mkmapview android-mapview mkannotation

我有一些地图视图控制器,我有一个自定义注释。

自定义注释代码:

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

@interface DisplayMapAnnotation : NSObject <MKAnnotation> {

    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle;
    NSString *detailID;

}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
@property (nonatomic, retain) NSString *detailID;

@end

#import "DisplayMapAnnotation.h"

@implementation DisplayMapAnnotation
@synthesize coordinate, title, subtitle, detailID;

- (NSString *) title
{

    return title;

}

- (NSString *) subtitle
{

    return subtitle;

}

- (NSString *) detailID
{

    return detailID;

}

- (id)initWithCoordinate:(CLLocationCoordinate2D) c 
{

    coordinate=c;
    return self;

}

- (void) dealloc
{

    [title release];
    [subtitle release];
    [detailID release];

    [super dealloc];

}

这是我创建注释的地图视图代码: 在viewDidLoad方法中,我做了这样的事情:

for (FeedItems *aItem in geoDataList) {

            [mapView setZoomEnabled:YES];
            [mapView setScrollEnabled:YES];
            MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
            region.center.latitude = [aItem.geoLat doubleValue];
            region.center.longitude = [aItem.geoLng doubleValue];
            region.span.longitudeDelta = kLatitudeDelta;
            region.span.latitudeDelta = kLongitudeDelta;
            [mapView setRegion:region animated:YES]; 



            ann = [[DisplayMapAnnotation alloc] init]; 
            ann.title = aItem.job;
            ann.subtitle = aItem.jobCompany; 
            ann.detailID = aItem.jobID;
            ann.coordinate = region.center; 
            [mapView addAnnotation:ann];

            [aItem release];
        }

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

    MKPinAnnotationView *pinView = nil; 

    if (annotation != self.mapView.userLocation) {

        static NSString *defaultPinID = @"com.invasivecode.pin";

        pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

        if (pinView == nil) 
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorRed; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;

        if (mapIdentifier == 0) {

            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            //NSInteger annotationValue = [annView indexOfObject:annotation];
            //rightButton.tag = annotationValue;
            [rightButton addTarget:self action:@selector(detailButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            pinView.rightCalloutAccessoryView = rightButton;

        }

    } else {

        [self.mapView.userLocation setTitle:@"your location"];

    }

    return pinView;

}

这是我的问题(这个方法):

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

    NSLog(@"Detail id: %@", view.annotation.detailID);

}

当我将记录详细信息时,Xcode会说:“在'id'类型的对象上找不到属性'detailID'”有什么问题?

1 个答案:

答案 0 :(得分:3)

它不知道注释是您的自定义注释:

NSLog(@"Detail id: %@", view.annotation.detailID);

你可以这样投它来使警告静音

NSLog(@"Detail id: %@", [(DisplayMapAnnotation*)view.annotation detailID]);

检查完毕后是否为DisplayMapAnnotation *

if ([view.annotation isKindOfClass:[DisplayMapAnnotation class]])