覆盖MKPointAnnotation类

时间:2014-12-15 16:58:06

标签: ios objective-c mapkit

我重写了MKAnnotation并向MKAnnotation类添加了两个属性(NSManagedObject和UIImage),但是这个代码没有用呢?

//  myAnnotation.h

#import <MapKit/MapKit.h>

@interface myAnnotation : NSObject<MKAnnotation>{
    NSManagedObject *Contact;
    UIImage *image;
    NSString *title;
    NSString *subtitle;
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic,copy) NSManagedObject *Contact;
@property (nonatomic, copy) UIImage *image;
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *subtitle;

@end

//  myAnnotation.m

#import "myAnnotation.h"

@implementation myAnnotation
@synthesize Contact,title,subtitle,coordinate,image;
@end

//  MapVC.h
#import "myAnnotation.h"
@interface MapVC : UIViewController<MKMapViewDelegate,UIActionSheetDelegate,MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate,UISearchBarDelegate>{
    MKMapView *mapview;
    myAnnotation *tmpContact;
}

//  MapVC.m
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];


    [mapview removeAnnotations:mapview.annotations];

    //add each object in Contacts entity to map view
    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    fetchRequest=[[NSFetchRequest alloc]initWithEntityName:@"Contacts"];
    fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:nil];




    for (NSManagedObject *info in fetchedObjects) {
        NSLog(@"Name: %@", [info valueForKey:@"name"]);

        //initializetion latitude and longitude
        aLng=[[info  valueForKey:@"lng"] doubleValue];
        aLat=[[info  valueForKey:@"lat"] doubleValue];
        //if latitude and longitude not null
        if(aLng && aLat && aLng!=0.0 &&aLat!=0.0)
        {
            //create a new Coordinate
            CLLocationCoordinate2D wimLocation;
            wimLocation.latitude=aLat;
            wimLocation.longitude=aLng;

            myAnnotation *myAnn=[myAnnotation alloc];
            myAnn.coordinate=wimLocation;
            myAnn.title=[info valueForKey:@"name"];
            myAnn.Contact=info;

            UIImage *image = [UIImage imageWithData:[info  valueForKey:@"photo"]];
            myAnn.image=image;

            //add create Annotation to mapview
            [self.mapview addAnnotation:myAnn];


        }
    }
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[myAnnotation class]])
    {
        static NSString *reuseId = @"ann";
        MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
        if (av == nil)
        {
            av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        }
        else
        {
            av.annotation = annotation;
        }

        myAnnotation *ann = (myAnnotation *)annotation;
        av.image = ann.image;

        return av;
    }

    //return nil (default view) if annotation is not our custom type
    return nil;
}

1 个答案:

答案 0 :(得分:0)

只需子类MKPointAnnotation或在自定义类中实现MKAnnotation协议。